degal/utils.py
changeset 120 55cb7fc9c8fb
parent 109 66a01c0806f1
child 128 f66bb9f6126a
--- a/degal/utils.py	Sun Jun 14 22:59:29 2009 +0300
+++ b/degal/utils.py	Sun Jun 14 23:43:40 2009 +0300
@@ -77,6 +77,26 @@
 lazy_load = LazyProperty
 lazy_load_iter = LazyIteratorProperty
 
+def first (iterable) :
+    """
+        Returns the first item from the iterable that evaluates to True, otherwise None.
+
+        >>> first((0, 1))
+        1
+        >>> first("abc")
+        'a'
+        >>> first(('', list(), (), False))
+        None
+    """
+
+    for item in iterable :
+        if item :
+            return item
+
+    else :
+        return None
+
+
 # testing
 if __name__ == '__main__' :
     import doctest