Skip to content

Commit

Permalink
Fix extract_arguments to allow nested dictionaries
Browse files Browse the repository at this point in the history
Also add a lot of comments to explain what is going on. There is an
assertion to verify the result has the correct form.

The example from comment should be made a proper test.
  • Loading branch information
lubomir committed Sep 29, 2015
1 parent fd4870b commit 6af97b7
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pdc_client/plugin_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,27 @@ def add_parser_arguments(parser, args, group=None, prefix=DATA_PREFIX):


def extract_arguments(args, prefix=DATA_PREFIX):
"""
Return a dict of arguments created by `add_parser_arguments`.
"""Return a dict of arguments created by `add_parser_arguments`.
If the key in `args` contains two underscores, a nested dictionary will be
created.
Input: {'foo__bar__baz': 1, 'foo__bar__quux': 2}
Output: {'foo': {'bar': {'baz': 1, 'quux': 2}}}
"""
data = {}
for key, value in args.__dict__.iteritems():
if key.startswith(prefix) and value is not None:
parts = key[len(prefix):].split('__')
# Think of `d` as a pointer into the resulting nested dictionary.
# The `for` loop iterates over all parts of the key except the last
# to find the proper dict into which the value should be inserted.
# If the subdicts do not exist, they are created.
d = data
for p in parts[:-1]:
d[p] = {}
d = d[p]
assert p not in d or isinstance(d[p], dict)
d = d.setdefault(p, {})
# At this point `d` points to the correct dict and value can be
# inserted.
d[parts[-1]] = value if value != '' else None
return data

0 comments on commit 6af97b7

Please sign in to comment.