--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/qmsk/utils.py Sat Jun 07 16:21:59 2014 +0300
@@ -0,0 +1,51 @@
+import types
+
+FLATTEN_TYPES = (tuple, list, types.GeneratorType)
+
+def flatten (*items):
+ """
+ Flatten common containers/iterables
+
+ >>> list(flatten())
+ []
+ >>> list(flatten(1, 2, 3))
+ [1, 2, 3]
+ >>> list(flatten((1, 2, 3)))
+ [1, 2, 3]
+ >>> list(flatten((1, 2), 3))
+ [1, 2, 3]
+ >>> list(flatten((1, ), [2], (i for i in [3])))
+ [1, 2, 3]
+ """
+
+ for item in items:
+ if item is None:
+ continue
+
+ elif isinstance(item, FLATTEN_TYPES):
+ for node in item:
+ yield from flatten(node)
+
+ else:
+ yield item
+
+def merge (*dicts, **kwargs):
+ """
+ Merge dicts.
+
+ >>> merge(foo=1, bar=2)
+ {'foo': 1, 'bar': 2}
+ >>> merge(dict(foo=1), bar=2)
+ {'foo': 1, 'bar': 2}
+ >>> merge(dict(foo=1), bar=2, foo=3)
+ {'foo': 3, 'bar': 2}
+ >>> merge(dict(foo=1), dict(bar=2), foo=3)
+ {'foo': 3, 'bar': 2}
+ >>> merge(dict(bar=2), dict(foo=1), foo=3)
+ {'foo': 3, 'bar': 2}
+ >>> merge(dict(foo=1), dict(foo=2), foo=3)
+ {'foo': 3}
+ """
+
+ # later duplicate keys override
+ return {k: v for d in (dicts + (kwargs, )) for k, v in d.items()}