Commit c26fae0d24b539ebe447a2120db1f7a89b04abef

Authored by selurvedu
1 parent 39cf3497

Simplify create_data_dict()

The type check of `path_to_value` was removed because
there is already one in `set_to_object()`.
The check for the value of the `value` argument was removed because
it makes no sense, since `None` is a valid value too.
... ... @@ -474,18 +474,23 @@ def merge_dicts(a, b):
474 474
475 475
476 476 def create_data_dict(path_to_value=None, value=None):
477   - data_dict = munchify({'data': {}})
478   - if isinstance(path_to_value, basestring) and value:
479   - list_items = re.search('\d+', path_to_value)
480   - if list_items:
481   - list_items = list_items.group(0)
482   - path_to_value = path_to_value.split('[' + list_items + ']')
483   - path_to_value.insert(1, '.' + list_items)
484   - set_to_object(data_dict, path_to_value[0], [])
485   - set_to_object(data_dict, ''.join(path_to_value[:2]), {})
486   - set_to_object(data_dict, ''.join(path_to_value), value)
487   - else:
488   - data_dict = set_to_object(data_dict, path_to_value, value)
  477 + """Create a dictionary with one key, 'data'.
  478 +
  479 + If `path_to_value` is not given, set the key's value
  480 + to an empty dictionary.
  481 + If `path_to_value` is given, set the key's value to `value`.
  482 + In case it's the latter and if `value` is not set,
  483 + the key's value is set to `None`.
  484 +
  485 + Please note that `path_to_value` is relative to the parent dictionary,
  486 + thus, you may need to prepend `data.` to your path string.
  487 +
  488 + To better understand how `path_to_value` is handled,
  489 + please refer to the `set_to_object()` function.
  490 + """
  491 + data_dict = {'data': {}}
  492 + if path_to_value:
  493 + data_dict = set_to_object(data_dict, path_to_value, value)
489 494 return data_dict
490 495
491 496
... ...
Please register or login to post a comment