Skip to main content

Python module for interacting with nested dicts as a single level dict with delimited keys.

Project description

Version Status Coverage

FlatDict is a dict object that allows for single level, delimited key/value pair mapping of nested dictionaries. You can interact with FlatDict like a normal dictionary and access child dicts as you normally would or with the composite key.

Examples

foo = {'foo': {'bar': 'baz', 'qux': 'corge'}}

is represented as:

{'foo:bar': 'baz',
 'foo:qux': 'corge'}

And can still be accessed as:

foo['foo']['bar']

and

foo['foo:bar']

Additionally, lists and tuples are also converted into dicts using enumerate().

For example:

d = {'list': ['a', 'b', 'c',]}

Will be flattened as follows:

flat = {'list:0': 'a', 'list:1': 'b', 'list:2': 'c'}

API

FlatDict has the same methods as dict in Python 2.7. In addition, it has a FlatDict.as_dict method which will return a pure nested dictionary from a FlatDict value.

Documentation is available at http://flatdict.readthedocs.io

Installation

$ pip install flatdict

Example Use

import flatdict

values = {'foo': {'bar': {'baz': 0,
                          'qux': 1,
                          'corge': 2},
                  'grault': {'baz': 3,
                             'qux': 4,
                             'corge': 5}},
          'garply': {'foo': 0, 'bar': 1, 'baz': 2, 'qux': {'corge': 3}}}

flat = flatdict.FlatDict(values)

print(flat['foo:bar:baz'])

flat['test:value:key'] = 10

del flat['test']

for key in flat:
    print(key)

for values in flat.itervalues():
    print(value)

print(repr(flat.as_dict()))

print(flat == flat.as_dict())

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

flatdict-2.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.

flatdict-2.0.1-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file flatdict-2.0.1.tar.gz.

File metadata

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

File hashes

Hashes for flatdict-2.0.1.tar.gz
Algorithm Hash digest
SHA256 07f617fef59fcbb751903ef0c7c293cc3b2988c813a941dd340579cc0b5f7c61
MD5 e27104ecdaee93ede00f9ee6fc19b093
BLAKE2b-256 2439618e15f58df82f2b036acf13104fceefd0d0c89e7214b28d1cd0bad9087d

See more details on using hashes here.

File details

Details for the file flatdict-2.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flatdict-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 566fe612f3b12b7571f015ddc337a5493e225a49b6473a3091b74efa8a879d96
MD5 13ec78a515513a155ac1f28e9eb0f344
BLAKE2b-256 b9dbe70acf237e51ec3b9dbec7c9af6ffaa21a2581e5ad85f027dbf4dac85142

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