Browse Source

Added index_singleton_list utility function.

Nashwan Azhari 9 years ago
parent
commit
3f20055569
1 changed files with 12 additions and 0 deletions
  1. 12 0
      coriolis/utils.py

+ 12 - 0
coriolis/utils.py

@@ -49,6 +49,18 @@ def ignore_exceptions(func):
     return _ignore_exceptions
     return _ignore_exceptions
 
 
 
 
+def index_singleton_list(lis):
+    """ Indexes the head of a single element list.
+    Raises a KeyError if the list is empty or its length is greater than 1.
+    """
+    if len(lis) == 0:
+        raise KeyError("Result list is empty.")
+    elif len(lis) > 1:
+        raise KeyError("More than one result in list: '%s'" % lis)
+
+    return lis[0]
+
+
 def retry_on_error(max_attempts=5, sleep_seconds=0,
 def retry_on_error(max_attempts=5, sleep_seconds=0,
                    terminal_exceptions=[]):
                    terminal_exceptions=[]):
     def _retry_on_error(func):
     def _retry_on_error(func):