Skip to main content

Python dict accessible by dot.

Project description

dictdot

Python dict accessible by dot.

Implemented by overriding some builtin methods of regular Python dict.

Example

from dictdot import dictdot

d = dictdot({"foo": 1})
d.bar = 1
assert d.foo == d["bar"]

Features

  • Can initialize dictdot the same way as dict.

    from dictdot import dictdot
    
    a = ["foo", "bar", "baz"]
    b = range(3)
    
    d = dictdot(zip(a, b))
    
  • Access items by dot notation:

    assert d.foo < d["bar"] < d.get("baz")
    
  • After initialization, can add items either by regular item assignment, or by dot notation:

    d.bar = {
        "nested": 1,
        "foo": None,
        "nest": [None],
    }
    
  • Function to find keys and values nested in the dict structure:

    # Find every key equal to "foo".
    assert list(dictdot.find(d, check_key="foo")) == [".foo", ".bar.foo"]
    
    # Find every value equal to None.
    assert list(dictdot.find(d, check_value=None)) == [".bar.foo", ".bar.nest[0]"]
    
    # Both key and value must evaluate to True.
    assert list(dictdot.find(d, check_key="foo", check_value=None)) == [".bar.foo"]
    assert list(dictdot.find(d, check_key="bar", check_value=1)) == []
    
  • Each nested dict is also a dictdot:

    assert type(d.bar) is dictdot
    

See the tests in the source code for more details about the behavior and usage.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dictdot-1.5.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

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