dotwiz package#

Submodules#

dotwiz.common module#

Common (shared) helpers and utilities.

dotwiz.main module#

Main module.

class dotwiz.main.DotWiz(input_dict={}, __set=<slot wrapper '__setitem__' of 'dict' objects>, **kwargs)[source]#

Bases: dict

DotWiz - a blazing fast dict subclass that also supports dot access notation.

Usage:

>>> from dotwiz import DotWiz
>>> dw = DotWiz({'key_1': [{'k': 'v'}], 'keyTwo': '5', 'key-3': 3.21})
>>> assert dw.key_1[0].k == 'v'
>>> assert dw.keyTwo == '5'
>>> assert dw['key-3'] == 3.21
to_dict(__items_fn=<method 'items' of 'dict' objects>)#

Recursively convert the DotWizPlus instance back to a dict.

update(input_dict={}, __set=<slot wrapper '__setitem__' of 'dict' objects>, **kwargs)#

Helper method to generate / update a DotWiz (dot-access dict) from a Python dict object, and optional keyword arguments.

dotwiz.main.make_dot_wiz(*args, **kwargs)[source]#

Helper function to create and return a DotWiz (dot-access dict) from an optional iterable object and keyword arguments.

Example:

>>> from dotwiz import make_dot_wiz
>>> make_dot_wiz([('k1', 11), ('k2', [{'a': 'b'}]), ('k3', 'v3')], y=True)
✫(y=True, k1=11, k2=[✫(a='b')], k3='v3')

dotwiz.plus module#

Dot Wiz Plus module.

class dotwiz.plus.DotWizPlus(input_dict={}, **kwargs)[source]#

Bases: dict

DotWizPlus - a blazing fast dict subclass that also supports dot access notation. This implementation enables you to turn special-cased keys into valid snake_case words in Python, as shown below.

>>> from dotwiz import DotWizPlus
>>> dw = DotWizPlus({'Key 1': [{'3D': {'with': 2}}], 'keyTwo': '5', 'r-2!@d.2?': 3.21})
>>> dw
✪(key_1=[✪(_3d=✪(with_=2))], key_two='5', r_2_d_2=3.21)
>>> assert dw.key_1[0]._3d.with_ == 2
>>> assert dw.key_two == '5'
>>> assert dw.r_2_d_2 == 3.21
>>> dw.to_dict()
{'Key 1': [{'3D': {'with': 2}}], 'keyTwo': '5', 'r-2!@d.2?': 3.21}
>>> dw.to_attr_dict()
{'key_1': [{'_3d': {'with_': 2}}], 'key_two': '5', 'r_2_d_2': 3.21}

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.

to_attr_dict()#

Recursively convert the DotWizPlus instance back to a dict, while preserving the lower-cased keys used for attribute access.

to_dict(__items_fn=<method 'items' of 'dict' objects>)#

Recursively convert the DotWizPlus instance back to a dict.

update(input_dict={}, **kwargs)#

Helper method to generate / update a DotWizPlus (dot-access dict) from a Python dict object, and optional keyword arguments.

dotwiz.plus.make_dot_wiz_plus(*args, **kwargs)[source]#

Helper function to create and return a DotWizPlus (dot-access dict) from an optional iterable object and keyword arguments.

Example:

>>> from dotwiz import make_dot_wiz_plus
>>> make_dot_wiz_plus([('k1', 11), ('k2', [{'a': 'b'}]), ('k3', 'v3')], y=True)
✪(y=True, k1=11, k2=[✪(a='b')], k3='v3')

Module contents#

dotwiz#

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

Sample Usage:

>>> from dotwiz import DotWiz
>>> dw = DotWiz({'this': {'works': {'for': [{'nested': {'values': True}}]}}},
...             the_answer_to_life=42)
>>> dw
✫(this=✫(works=✫(for=[✫(nested=✫(values=True))])), the_answer_to_life=42)
>>> dw.this.works['for'][0].nested.values
True
>>> dw.the_answer_to_life
42

For full documentation and more advanced usage, please see <https://dotwiz.readthedocs.io>.

copyright
  1. 2022 by Ritvik Nag.

license

MIT, see LICENSE for more details.

class dotwiz.DotWiz(input_dict={}, __set=<slot wrapper '__setitem__' of 'dict' objects>, **kwargs)[source]#

Bases: dict

DotWiz - a blazing fast dict subclass that also supports dot access notation.

Usage:

>>> from dotwiz import DotWiz
>>> dw = DotWiz({'key_1': [{'k': 'v'}], 'keyTwo': '5', 'key-3': 3.21})
>>> assert dw.key_1[0].k == 'v'
>>> assert dw.keyTwo == '5'
>>> assert dw['key-3'] == 3.21
to_dict(__items_fn=<method 'items' of 'dict' objects>)#

Recursively convert the DotWizPlus instance back to a dict.

update(input_dict={}, __set=<slot wrapper '__setitem__' of 'dict' objects>, **kwargs)#

Helper method to generate / update a DotWiz (dot-access dict) from a Python dict object, and optional keyword arguments.

class dotwiz.DotWizPlus(input_dict={}, **kwargs)[source]#

Bases: dict

DotWizPlus - a blazing fast dict subclass that also supports dot access notation. This implementation enables you to turn special-cased keys into valid snake_case words in Python, as shown below.

>>> from dotwiz import DotWizPlus
>>> dw = DotWizPlus({'Key 1': [{'3D': {'with': 2}}], 'keyTwo': '5', 'r-2!@d.2?': 3.21})
>>> dw
✪(key_1=[✪(_3d=✪(with_=2))], key_two='5', r_2_d_2=3.21)
>>> assert dw.key_1[0]._3d.with_ == 2
>>> assert dw.key_two == '5'
>>> assert dw.r_2_d_2 == 3.21
>>> dw.to_dict()
{'Key 1': [{'3D': {'with': 2}}], 'keyTwo': '5', 'r-2!@d.2?': 3.21}
>>> dw.to_attr_dict()
{'key_1': [{'_3d': {'with_': 2}}], 'key_two': '5', 'r_2_d_2': 3.21}

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.

to_attr_dict()#

Recursively convert the DotWizPlus instance back to a dict, while preserving the lower-cased keys used for attribute access.

to_dict(__items_fn=<method 'items' of 'dict' objects>)#

Recursively convert the DotWizPlus instance back to a dict.

update(input_dict={}, **kwargs)#

Helper method to generate / update a DotWizPlus (dot-access dict) from a Python dict object, and optional keyword arguments.

dotwiz.make_dot_wiz(*args, **kwargs)[source]#

Helper function to create and return a DotWiz (dot-access dict) from an optional iterable object and keyword arguments.

Example:

>>> from dotwiz import make_dot_wiz
>>> make_dot_wiz([('k1', 11), ('k2', [{'a': 'b'}]), ('k3', 'v3')], y=True)
✫(y=True, k1=11, k2=[✫(a='b')], k3='v3')
dotwiz.make_dot_wiz_plus(*args, **kwargs)[source]#

Helper function to create and return a DotWizPlus (dot-access dict) from an optional iterable object and keyword arguments.

Example:

>>> from dotwiz import make_dot_wiz_plus
>>> make_dot_wiz_plus([('k1', 11), ('k2', [{'a': 'b'}]), ('k3', 'v3')], y=True)
✪(y=True, k1=11, k2=[✪(a='b')], k3='v3')
dotwiz.set_default_for_missing_keys(default=None, overwrite=False)[source]#

Modifies DotWiz and DotWizPlus to add a custom __getattr__, so that accessing missing or non-existing attributes (keys) returns default instead of raising an AttributeError.

This provides a handy alternative to the builtin hasattr.

For more details, see https://github.com/rnag/dotwiz/issues/14.

Example:

>>> from dotwiz import DotWiz, set_default_for_missing_keys
>>> set_default_for_missing_keys('test')
>>> dw = DotWiz(hello='world!')
>>> assert dw.hello == 'world!'
>>> assert dw.world == 'test'
Parameters
  • default – The default value to return for missing or non-existing attributes (keys).

  • overwrite – True to overwrite a class’s __getattr__() method, if one already exists; defaults to False.