Skip to main content

Dot Wiz

https://img.shields.io/pypi/v/dotwiz.svg https://img.shields.io/pypi/pyversions/dotwiz.svg https://codecov.io/gh/rnag/dotwiz/branch/main/graph/badge.svg?token=J3YW230U8Z https://github.com/rnag/dotwiz/actions/workflows/dev.yml/badge.svg Updates

A blazing fast dict subclass that enables dot access notation via Python attribute style. Nested dict and list values are automatically transformed as well.


Assume you have a simple dict object, with dynamic keys:

>>> my_dict = {'this': {'dict': {'has': [{'nested': {'data': True}}]}}}

If the goal is to access a nested value, you could do it like this:

>>> my_dict['this']['dict']['has'][0]['nested']['data']
True

Or, using DotWiz:

>>> from dotwiz import DotWiz
>>> dw = DotWiz(my_dict)
>>> dw.this.dict.has[0].nested.data
True

Note: This library can also make inaccessible keys safe – check out an example with DotWizPlus.

Install

$ pip install dotwiz

Usage

DotWiz

Here is an example of how to create and use a DotWiz object:

from dotwiz import DotWiz

dw = DotWiz({'this': {'works': {'for': [{'nested': {'values': True}}]}}},
            the_answer_to_life=42)

print(dw)
# >  ✫(this=✫(works=✫(for=[✫(nested=✫(values=True))])),
#      the_answer_to_life=42)

assert dw.this.works['for'][0].nested.values  # True
assert dw.the_answer_to_life == 42

print(dw.to_dict())
# >  {'this': {'works': {'for': [{'nested': {'values': True}}]}},
#     'the_answer_to_life': 42}

Using make_dot_wiz allows you to pass in an iterable object when creating a DotWiz object:

from dotwiz import make_dot_wiz

dw = make_dot_wiz([('hello, world!', 123), ('easy: as~ pie?', True)],
                  AnyKey='value')

print(dw)
#> ✫(AnyKey='value', hello, world!=123, easy: as~ pie?=True)

assert dw['hello, world!'] == 123
assert dw['easy: as~ pie?']
assert dw.AnyKey == 'value'

DotWizPlus

DotWiz+ enables you to turn special-cased keys, such as names with spaces, into valid snake_case words in Python, as shown below. Also see the note on Issues with Invalid Characters below.

from dotwiz import DotWizPlus

my_dict = {'THIS': {'1': {'is': [{'For': {'AllOf': {'My !@ Fans!': True}}}]}}}
dw = DotWizPlus(my_dict)

print(dw)
#> ✪(this=✪(_1=✪(is_=[✪(for_=✪(all_of=✪(my_fans=True)))])))

# True
assert dw.this._1.is_[0].for_.all_of.my_fans

# alternatively, you can access it like a dict with the original keys:
assert dw['THIS']['1']['is'][0]['For']['AllOf']['My !@ Fans!']

print(dw.to_dict())
# {'THIS': {'1': {'is': [{'For': {'AllOf': {'My !@ Fans!': True}}}]}}}

print(dw.to_attr_dict())
# {'this': {'_1': {'is_': [{'for_': {'all_of': {'my_fans': True}}}]}}}

Issues with Invalid Characters

A key name in the scope of the DotWizPlus implementation must be:

  • a valid, lower- and snake- cased identifier in python.

  • not a reserved keyword, such as for or class.

  • not override dict method declarations, such as items, get, or values.

In the case where your key name does not conform, the library will mutate your key to a safe, snake-cased format.

Spaces and invalid characters are replaced with _. In the case of a key beginning with an int, a leading _ is added. In the case of a keyword or a dict method name, a trailing _ is added. Keys that appear in different cases, such as myKey or My-Key, will all be converted to a snake case variant, my_key in this example.

Finally, check out this example which brings home all that was discussed above.

Features

  • TODO

Benchmarks

Check out the Benchmarks section in the docs for more info.

Using a dot-access approach such as DotWiz can be up to 100x faster than with make_dataclass from the dataclasses module.

It’s also about 5x faster to create a DotWiz from a dict object as compared to other libraries such as prodict – or close to 15x faster than creating a Box – and up to 10x faster in general to access keys by dot notation – or almost 30x faster than accessing keys from a DotMap.

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change.

Check out the Contributing section in the docs for more info.

Credits

This package was created with Cookiecutter and the rnag/cookiecutter-pypackage project template.

Metadata

Release files for dotwiz 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dotwiz 0.5.0
File Size Uploaded Provenance
dotwiz-0.5.0.tar.gz 25.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dotwiz 0.5.0
File Interpreter ABI Platform
dotwiz-0.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 39.3 kB

Release files / dotwiz-0.5.0.tar.gz

Download URL dotwiz-0.5.0.tar.gz
Size 25.3 kB
Tags Source
SHA-256 checksum
How to use checksums
606b6a15f66e8ead7da0f4ca1d664f2fce1a83b44936560be901c319dadf7cb3
BLAKE2b-256 checksum
How to use checksums
09d2d5c560d4fdb4d4ab3163d38ee39b1534655bf586af218d9fe19fcd3bff97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / dotwiz-0.5.0-py2.py3-none-any.whl

Download URL dotwiz-0.5.0-py2.py3-none-any.whl
Size 13.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
10d6b1106ad78acc67ad3546d479e14a650eef405833090e83756ac257beac79
BLAKE2b-256 checksum
How to use checksums
efe8341d51e5ffc75a583d61378d6acc4263ecf31a4c386f347fcab38b129b03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for dotwiz 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page