Unflatten dict to dict with nested dict/arrays
Project description
Description
This package provides a function which can unpack a flat dictionary into a structured dict with nested sub-dicts and/or sub-lists.
Development takes place on github. The package is installable from PyPI
Synopsis
Nested dicts:
>>> from unflatten import unflatten >>> unflatten({'foo.bar': 'val'}) {'foo': {'bar': 'val'}}
Nested list:
>>> unflatten({'foo[0]': 'x', 'foo[1]': 'y'}) {'foo': ['x', 'y']}
Nested lists and dicts, intermixed:
>>> unflatten({ ... 'foo[0][0]': 'a', ... 'foo[0][1]': 'b', ... 'foo[1].x': 'c', ... }) {'foo': [['a', 'b'], {'x': 'c'}]}
Notes
Unflatten take a single argument which should either be a dict (or an object with a dict-like .items() or .iteritems() method) or a sequence of (key, value) pairs. All keys in the dict or sequence must be strings. (Under python 2, keys must be instances of basestring; under python 3, keys just be instances of str.)
Unflatten always returns a dict. By way of example:
>>> unflatten([('[0]', 'x')]) {'': ['x']}
For list-valued nodes, all indexes must be present in the input (flattened) mapping, otherwise a ValueError will be thrown:
>>> unflatten({'a[0]': 'x', 'a[2]': 'y'}) Traceback (most recent call last): ... ValueError: missing key 'a[1]'
History
Release 0.1b1 (2018-01-09)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for unflatten-0.1b1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d04508afc6c79d248de5538662811b3ebfd0228f6843d91f6fd2f11a6d6cab4d |
|
MD5 | f92aff2293806b09b3ab1b55b53e5fe2 |
|
BLAKE2b-256 | 0de596abd4b6af62e8b12563368d310c04131cfacabdd19e17214a261e60755c |