Skip to main content

dotty-dictionary

PyPI version Testsuite Python version Project Status Supported Interpreters License

dotty-dictionary is a Python library that provides a dictionary-like object that allows you to access nested dictionaries using dot notation.

dotty-dictionary is a fork of pawelzny/dotty_dict that provides additional features and improvements.

Features

  • Simple wrapper around python dictionary and dict like objects
  • Two wrappers with the same dict are considered equal
  • Access to deeply nested keys with dot notation: dot['deeply.nested.key']
  • Create, read, update and delete nested keys of any length
  • Expose all dictionary methods like .get, .pop, .keys and other
  • Access dicts in lists by index dot['parents.0.first_name']
  • Support for setting value in multidimensional lists
  • Support for accessing lists with slices
  • Support for flattening nested dictionary keys from a dotty dictionary and vice versa
  • Support for iteration over nested dictionary keys (e.g. for key in dotty_dict_instance)

Installation

pip install dotty-dictionary

Quick Example

Create new dotty using factory function.

from dotty_dictionary import dotty
dot = dotty({'plain': {'old': {'python': 'dictionary'}}})
dot['plain.old']
{'python': 'dictionary'}

You can start with empty dotty

from dotty_dictionary import dotty
dot = dotty() # Alias: `Dotty.empty()`
dot['very.deeply.nested.thing'] = 'spam'
dot
Dotty(dictionary={'very': {'deeply': {'nested': {'thing': 'spam'}}}}, separator='.', esc_char='\\')

dot['very.deeply.spam'] = 'indeed'
dot
Dotty(dictionary={'very': {'deeply': {'nested': {'thing': 'spam'}, 'spam': 'indeed'}}}, separator='.', esc_char='\\')

del dot['very.deeply.nested']
dot
Dotty(dictionary={'very': {'deeply': {'spam': 'indeed'}}}, separator='.', esc_char='\\')

dot.get('very.not_existing.key')
None

More examples can be found in the examples directory.

[!NOTE]
Using integer in dictionary keys will be treated as embedded list index.

Flattening and Unflattening

You can utilize to_flat_dict and from_flat_dict to convert dotty to and from flat dictionary.

from dotty_dictionary import Dotty
dot = Dotty.from_flat_dict({'very.deeply.nested.thing': 'spam', 'very.deeply.spam': 'indeed'})
dot
Dotty(dictionary={'very': {'deeply': {'nested': {'thing': 'spam'}, 'spam': 'indeed'}}}, separator='.', esc_char='\\')

dot.to_flat_dict()
{'very.deeply.nested.thing': 'spam', 'very.deeply.spam': 'indeed'}

Custom Types && Encoders

By default, dotty-dictionary only considers dict as a mapping type, and list as a sequence type and will provide a dot notation access for them. However, you can also provide custom types to be considered as mapping or sequence types.

from collections.abc import MutableMapping
from dataclasses import dataclass
from dotty_dictionary import Dotty, DottyEncoder


@dataclass
class User(MutableMapping):
    name: str
    age: int

class CustomJSONEncoder(DottyEncoder):
    def default(self, obj):
        if isinstance(obj, User):
            return {"name": obj.name, "age": obj.age}
        return super().default(obj)

dictionary = {
    "a": { 
        "b": { "c": 1, "d": 2 },
        "e": (3, {"f": 4}, (5, 6, 7)), # Has Tuple
    },
    "g": 8,
    "h": User(name="John", age=25), # Has Custom Dataclass
}
dot = Dotty(
    dictionary,
    mapping_types=(dict, User),
    sequence_types=(list, tuple),
    json_encoder=CustomJSONEncoder,
)

dot["a.e.1.f"]
4

dot["h.name"]
"John"

dot["h.age"] = 26
dot["h.age"]
26

Full example can be found on tests/test_dotty_custom_types.py

Contributing

Any contribution is welcome! Check out CONTRIBUTING.md and CODE_OF_CONDUCT.md for more information on how to get started.

License

dotty-dictionary is licensed under a MIT License.

Download files

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

Source Distribution

dotty_dictionary-1.2.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

dotty_dictionary-1.2.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file dotty_dictionary-1.2.0.tar.gz.

File metadata

  • Download URL: dotty_dictionary-1.2.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.5.0-1015-azure

File hashes

Hashes for dotty_dictionary-1.2.0.tar.gz
Algorithm Hash digest
SHA256 035873367519439ab17af8a1b1fe84bd8bf3e7da77f2521c49e86f32c4bcc03a
MD5 817b4ed403d5ceb1029a2d65bc189ae6
BLAKE2b-256 73dd5488d458b1e56bd96f2ddd400a989180f67eebec6ca158f390ffbe1743aa

See more details on using hashes here.

File details

Details for the file dotty_dictionary-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: dotty_dictionary-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Linux/6.5.0-1015-azure

File hashes

Hashes for dotty_dictionary-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a763d7a5146975ef03e5aa2b978aee43df97140adad9fee4c68f6d80fd36fbb
MD5 a5be68db08f3a9af2d1b872240a551ec
BLAKE2b-256 e9677a01141a9f59925980e3e0758705ad52cf56994bbbf6c346fe551232b4c6

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 files

1.1.1

2 files

1.1.0

2 files

Supported by

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