Package: dotwiz
Ablazing fastdictsubclass that enablesdot accessnotation via Python
attribute style. Nesteddictandlistvalues are automatically
transformed as well.Documentation:https://dotwiz.readthedocs.io.Assume you have a simpledictobject, 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']
TrueOr, usingDotWiz:>>> from dotwiz import DotWiz
>>> dw = DotWiz(my_dict)
>>> dw.this.dict.has[0].nested.data
TrueNote: This library can also make inaccessible keys safe – check outan examplewithDotWizPlus.Install$pipinstalldotwizUsageDotWizHere is an example of how to create and use aDotWizobject:fromdotwizimportDotWizdw=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)assertdw.this.works['for'][0].nested.values# Trueassertdw.the_answer_to_life==42print(dw.to_dict())# >  {'this': {'works': {'for': [{'nested': {'values': True}}]}},#     'the_answer_to_life': 42}Usingmake_dot_wizallows you to pass in an iterable object when
creating aDotWizobject:fromdotwizimportmake_dot_wizdw=make_dot_wiz([('hello, world!',123),('easy: as~ pie?',True)],AnyKey='value')print(dw)#> ✫(AnyKey='value', hello, world!=123, easy: as~ pie?=True)assertdw['hello, world!']==123assertdw['easy: as~ pie?']assertdw.AnyKey=='value'DotWizPlusDotWiz+enables you to turn special-cased keys, such as names with spaces,
into validsnake_casewords in Python, as shown below. Also see the note
onIssues with Invalid Charactersbelow.fromdotwizimportDotWizPlusmy_dict={'THIS':{'1':{'is':[{'For':{'AllOf':{'My !@ Fans!':True}}}]}}}dw=DotWizPlus(my_dict)print(dw)#> ✪(this=✪(_1=✪(is_=[✪(for_=✪(all_of=✪(my_fans=True)))])))# Trueassertdw.this._1.is_[0].for_.all_of.my_fans# alternatively, you can access it like a dict with the original keys:assertdw['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 CharactersA key name in the scope of theDotWizPlusimplementation must be:a valid,lower-andsnake-casedidentifierin python.not a reservedkeyword, such asfororclass.not overridedictmethod declarations, such asitems,get, orvalues.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 anint, a leading_is added.
In the case of akeywordor adictmethod name, a trailing_is added. Keys that appear in different cases, such
asmyKeyorMy-Key, will all be converted to
asnake casevariant,my_keyin this example.Finally, check outthis examplewhich brings home all
that was discussed above.FeaturesTODOBenchmarksCheck out theBenchmarkssection in the docs for more info.Using adot-accessapproach such asDotWizcan be up
to100xfaster than withmake_dataclassfrom thedataclassesmodule.It’s also about5xfaster to create aDotWizfrom adictobject
as compared to other libraries such asprodict– or close to15xfaster
than creating aBox– and up to10xfaster in general to access keys
bydotnotation – or almost30xfaster than accessing keys from aDotMap.ContributingContributions are welcome! Open a pull request to fix a bug, oropen an issueto discuss a new feature or change.Check out theContributingsection in the docs for more info.CreditsThis package was created withCookiecutterand thernag/cookiecutter-pypackageproject template.