Skip to main content

Python dict accessible by dot.

Project description

Python dict accessible by dot.

Usage:

from dictdot import dictdot

d = dictdot()

d["a"] = 1
assert d["a"] == d.a

d.b = 2
assert d["b"] == d.b

# builtin attributes are not overriden.
d.items = "foo"
assert d.items != d["items"]
assert hasattr(d.items, "__call__")

# d["NA"] will raise KeyError, but
assert d.NA is None

# delete by attribute name.
del d.b
assert "b" not in d.keys()

# `copy` method returns a dictdot.
d2 = d.copy()
assert d2 == d
assert type(d2) is dictdot

# nested dicts are also dictdot.
d = dictdot(
    a={"x": 0},
    b=[{"y": 1}],
)
assert d.a.x == d.a["x"] == d["a"].x
assert d.b[0].y == d["b"][0]["y"]

# even when added after init. non-dict values are not modified.
d.c = [{"z": 2}, 2]
assert d.c[0].z == d.c[1]

# recursive dicts still work.
d.c.append(d)
assert d == d.c[-1] == d.c[-1].c[-1]

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.3.1-py3-none-any.whl (14.6 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