nicholasmireles / DotDict

A simple Python library to make chained attributes possible.
GNU General Public License v3.0
231 stars 3 forks source link

Add a recursive constructor #6

Closed pkoryzna closed 7 months ago

pkoryzna commented 1 year ago

new from_dict() factory method allows creating DotDicts from nested Python dicts, such as API responses. Usage example:

>>> from dotdict import DotDict
>>> nested = {"A": {"B": {"C": {"D": 1}}}}
>>> abcd = DotDict.from_dict(nested)
>>> abcd.A
DotDict(B=DotDict(C=DotDict(D=1)))
>>> abcd.A.B
DotDict(C=DotDict(D=1))
>>> abcd.A.B.C
DotDict(D=1)
>>> abcd.A.B.C.D
1

Fixes issue #3