Skip to main content

Unflatten dict to dict with nested dict/arrays

Project description

Latest Version Python versions build status

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]'

See Also

The morph and flattery packages purport to implement similar functions.

Authors

Jeff Dairiki

History

Release 0.1 (2018-01-17)

No code changes.

This package is now deemed “production ready” (though your mileage may vary.)

Release 0.1b1 (2018-01-09)

Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unflatten-0.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unflatten-0.1-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file unflatten-0.1.tar.gz.

File metadata

  • Download URL: unflatten-0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for unflatten-0.1.tar.gz
Algorithm Hash digest
SHA256 3a44a1baf20a299a2c75a9a1761fcdbff1b40d3b6145a71f3432188c7f05afa3
MD5 1eec19469bdee6740b5e2c93c126dd81
BLAKE2b-256 0c3a4bb8bb8c46bff5b301903d84ebb3d92a4c542363de7c04dc09957137a0a6

See more details on using hashes here.

File details

Details for the file unflatten-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for unflatten-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5ace25f23d07dc6f56fdd4e0d033cdea56d97e0158781aef5f003969ed71aa1f
MD5 e4c4b13c77d168c6f73d01b1c265a0a9
BLAKE2b-256 d264267a65ec55a292e37eadd30f67343511096a13aa9d27c5beb0f03ff1230d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page