* Add file for misc tools, now containing make_iter to ensure something is iteratable.
authorMatthijs Kooijman <matthijs@stdin.nl>
Thu, 31 Jan 2008 16:39:33 +0000 (17:39 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Thu, 31 Jan 2008 16:39:33 +0000 (17:39 +0100)
tools/misc.py [new file with mode: 0644]

diff --git a/tools/misc.py b/tools/misc.py
new file mode 100644 (file)
index 0000000..110c370
--- /dev/null
@@ -0,0 +1,15 @@
+"""
+Makes an iteratable object out of the given value. If value is already
+iterable, it is simply returned. An exception is made for strings, which
+are treated as non-iterable (It is assumed that you never want to have a
+list of characters). For non iteratable values or strings, a list
+containing only value is returned.
+"""
+def make_iter(value):
+    if (not isinstance(value, basestring)):
+        try:
+            iter(value)
+            return value
+        except TypeError:
+            pass
+    return [value]