Skip to main content

DotWiz is a blazing fast dict subclass that enables accessing (nested) keys in dot notation.

Project description

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

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

DotWizPlus 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.plus 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-cased identifier in python, and also not a reserved keyword such as for or class. In the case where your key name does not conform, the library will mutate your key to a safe, lower-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, 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.

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.

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

dotwiz-0.3.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

dotwiz-0.3.0-py2.py3-none-any.whl (12.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dotwiz-0.3.0.tar.gz.

File metadata

  • Download URL: dotwiz-0.3.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for dotwiz-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3d9aafc926298f47b5ea70d1d4a6ee5477760bec6974189838fc76d47389529c
MD5 e96a7b8581a4c1d0d601524df289ded2
BLAKE2b-256 48d3a3a604059b778afd790e87fe53e5a715528ace03c1a0428eedbd8b8366a5

See more details on using hashes here.

File details

Details for the file dotwiz-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: dotwiz-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for dotwiz-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 444e3bacd3fc17fb91e89ba8178d80222fa62ef820e75e26d9ae576379c71817
MD5 6969a105dc420909731e71a360fb5df9
BLAKE2b-256 0ab4662799ba0c8b10645d95022275ea74115c61a001b28c8c55d5f5bfc06087

See more details on using hashes here.

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