Commits

Mike Orr committed 2f3bfb0

New module webhelpers.misc

Comments (0)

Files changed (2)

unfinished/opener.py

+"""A unified front end for opening plain or compressed files.
+
+This is in 'unfinished' because a smarter opener could also decode Unicode,
+and check the file's magic number rather depending on the filename
+extension.  Are these worth implementing?  Would they be more worthwhile
+under Python 3, which might provide a Unicode-aware opener for bz2 and gz?
+"""
+
+def smart_open(filename, mode):
+    """Unified front end for opening plain files and compressed files."""
+    if   filename.endswith(".bz2"):
+        import bz2
+        opener = bz2.BZ2File
+    elif filename.endswith(".gz"):
+        import gzip
+        opener = gzip.open
+    else:
+        opener = open
+    return opener(filename, mode)
+    

webhelpers/misc.py

+"""Helpers that are neither text, numeric, container, or date.
+"""
+
+def all(seq, pred=None):
+    """Is ``pred(elm)`` true for all elements?
+
+    With the default predicate, this is the same as Python 2.5's ``all()``
+    function; i.e., it returns true if all elements are true.
+
+    From recipe in itertools docs.
+    """
+    for elem in itertools.ifilter(pred, seq):
+        return True
+    return False
+
+def any(seq, pred=None):
+    """Is ``pred(elm)`` is true for any element?
+
+    With the default predicate, this is the same as Python 2.5's ``any()``
+    function; i.e., it returns true if any element is true.
+
+    From recipe in itertools docs.
+    """
+    for elem in itertoos.ifilterfalse(pred, seq):
+        return False
+    return True
+
+def no(seq, pred=None):
+    """Is ``pred(elm)`` true for no elements?
+
+    With the default predicate, this returns true if all elements are false.
+
+    From recipe in itertools docs.
+    """
+    for elem in ifilter(pred, seq):
+        return False
+    return True
+
+def count_true(seq, pred=lambda x: x):
+    """How many elements is ``pred(elm)`` true for?
+
+    With the default predicate, this counts the number of true elements.
+
+    This is equivalent to the ``itertools.quantify`` recipe, which I couldn't
+    get to work.
+    """
+    ret = 0
+    for x in seq:
+        if pred(x):
+            ret += 1
+    return ret
+
+def convert_or_none(value, type_):
+    """Return the value converted to the type, or None if error.
+       ``type_`` may be a Python type or any function.
+    """
+    try:
+        return type_(value)
+    except Exception:
+        return None
+