Skip to main content

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

Project description

Version Status Coverage License

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

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

Installation

$ pip install flatdict

Example Use

import pprint

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 value in flat.itervalues():
    print(value)

pprint.pprint(flat.as_dict())

pprint.pprint(dict(flat))

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-3.4.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

flatdict-3.4.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flatdict-3.4.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for flatdict-3.4.0.tar.gz
Algorithm Hash digest
SHA256 bcfb4ea75d7465346a6072f477e795bbbf1bbb6c3da9cf3749b0d3d31988dd28
MD5 5b51da53193dcc5c5d4f4ca669a9442a
BLAKE2b-256 1cd3d2613f5200a634eeca8a3f6b538c4a44e88b8f54fc8fdbe97da63fdfb488

See more details on using hashes here.

File details

Details for the file flatdict-3.4.0-py3-none-any.whl.

File metadata

  • Download URL: flatdict-3.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.1

File hashes

Hashes for flatdict-3.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a60aaa27de0dfac2098edaffc109ee3f8eeeb47dcab68aa9140f0d9e24a6aa25
MD5 2ee521a350d6ccf70dea21ee0802b726
BLAKE2b-256 7d928ce0b794af38db60d87d6c8a11f0f0f2c20ede53736a86b052b99884bc6d

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