summaryrefslogtreecommitdiff
path: root/factory/utils.py
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2012-04-09 13:31:13 +0200
committerRaphaël Barrois <raphael.barrois@polyconseil.fr>2012-04-15 10:05:19 +0200
commit184dd0516267c58370d6a88afb1c1ce894b2b7c1 (patch)
tree939be03111e6bbf31ebae88763178df5b4d5ed43 /factory/utils.py
parent223c31ce7dd962f43c580c89fa1966939557bc5c (diff)
downloadfactory-boy-184dd0516267c58370d6a88afb1c1ce894b2b7c1.tar
factory-boy-184dd0516267c58370d6a88afb1c1ce894b2b7c1.tar.gz
Cleaner extract_dict: allow excluded keys.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'factory/utils.py')
-rw-r--r--factory/utils.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/factory/utils.py b/factory/utils.py
index 6c6fd7d..2fcd7ff 100644
--- a/factory/utils.py
+++ b/factory/utils.py
@@ -25,7 +25,7 @@
#: (subfactory_name, subfactory_field) tuple.
ATTR_SPLITTER = '__'
-def extract_dict(prefix, kwargs, pop=True):
+def extract_dict(prefix, kwargs, pop=True, exclude=()):
"""Extracts all values beginning with a given prefix from a dict.
Can either 'pop' or 'get' them;
@@ -34,6 +34,7 @@ def extract_dict(prefix, kwargs, pop=True):
prefix (str): the prefix to use for lookups
kwargs (dict): the dict from which values should be extracted
pop (bool): whether to use pop (True) or get (False)
+ exclude (iterable): list of prefixed keys that shouldn't be extracted
Returns:
A new dict, containing values from kwargs and beginning with
@@ -43,6 +44,9 @@ def extract_dict(prefix, kwargs, pop=True):
prefix = prefix + ATTR_SPLITTER
extracted = {}
for key in kwargs.keys():
+ if key in exclude:
+ continue
+
if key.startswith(prefix):
new_key = key[len(prefix):]
if pop:
@@ -63,10 +67,21 @@ def declength_compare(a, b):
return cmp(a, b)
-def multi_extract_dict(prefixes, kwargs, pop=True):
- """Extracts all values from a given list of prefixes."""
+def multi_extract_dict(prefixes, kwargs, pop=True, exclude=()):
+ """Extracts all values from a given list of prefixes.
+
+ Arguments have the same meaning as for extract_dict.
+
+ Returns:
+ dict(str => dict): a dict mapping each prefix to the dict of extracted
+ key/value.
+ """
results = {}
+ exclude = list(exclude)
for prefix in sorted(prefixes, cmp=declength_compare):
- extracted = extract_dict(prefix, kwargs, pop=pop)
+ extracted = extract_dict(prefix, kwargs, pop=pop, exclude=exclude)
results[prefix] = extracted
+ exclude.extend(
+ ['%s%s%s' % (prefix, ATTR_SPLITTER, key) for key in extracted])
+
return results