summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorNaveed Massjouni <naveedm9@gmail.com>2011-03-24 19:34:52 -0400
committerNaveed Massjouni <naveedm9@gmail.com>2011-03-24 19:34:52 -0400
commit5a9b13465822bb214f9e9cc9afff929664cbd1c7 (patch)
tree44ff422d4d5506aa532b6b19af9a77f9ae0a99c7 /nova/utils.py
parent74c226c564d5357b8b09edc67cc0bdfec6b8d871 (diff)
parent4e179b4fa9ab35dc50486e7f42e1dc6d06b74c81 (diff)
Merge from trunk
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py
index fa01f2c94..3f6f9fc8a 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -665,6 +665,48 @@ def get_from_path(items, path):
return get_from_path(results, remainder)
+def flatten_dict(dict_, flattened=None):
+ """Recursively flatten a nested dictionary"""
+ flattened = flattened or {}
+ for key, value in dict_.iteritems():
+ if hasattr(value, 'iteritems'):
+ flatten_dict(value, flattened)
+ else:
+ flattened[key] = value
+ return flattened
+
+
+def partition_dict(dict_, keys):
+ """Return two dicts, one containing only `keys` the other containing
+ everything but `keys`
+ """
+ intersection = {}
+ difference = {}
+ for key, value in dict_.iteritems():
+ if key in keys:
+ intersection[key] = value
+ else:
+ difference[key] = value
+ return intersection, difference
+
+
+def map_dict_keys(dict_, key_map):
+ """Return a dictionary in which the dictionaries keys are mapped to
+ new keys.
+ """
+ mapped = {}
+ for key, value in dict_.iteritems():
+ mapped_key = key_map[key] if key in key_map else key
+ mapped[mapped_key] = value
+ return mapped
+
+
+def subset_dict(dict_, keys):
+ """Return a dict that only contains a subset of keys"""
+ subset = partition_dict(dict_, keys)[0]
+ return subset
+
+
def check_isinstance(obj, cls):
"""Checks that obj is of type cls, and lets PyLint infer types"""
if isinstance(obj, cls):