Added a filter method to dicts
authorSverre Rabbelier <srabbelier@gmail.com>
Sun, 16 Nov 2008 15:50:05 +0000
changeset 486 ec6b50f48d3b
parent 485 2865922ea06a
child 487 1a7591ff0051
Added a filter method to dicts
app/soc/logic/dicts.py
--- a/app/soc/logic/dicts.py	Sun Nov 16 15:26:40 2008 +0000
+++ b/app/soc/logic/dicts.py	Sun Nov 16 15:50:05 2008 +0000
@@ -87,3 +87,23 @@
     result[key] = value
 
   return result
+
+def rename(target, keys):
+  """Returns a dict containing only the key/value pairs from keys
+
+  The keys from target will be looked up in keys, and the corresponding
+  value from keys will be used instead. If a key is not found, it is skipped.
+
+  Args:
+    target: the dictionary to filter
+    keys: the fields to filter
+  """
+
+  result = {}
+
+  for key, value in target.iteritems():
+    if key in keys:
+      new_key = keys[key]
+      result[new_key] = value
+
+  return result