Earlier quoted context omitted.
I enjoy how much complete control Python enables. It’s fun to turn it into what resembles a different language. I want to make a library where you never ever call functions with fn() notation. But just utilize accessors and absolutely bastardize dicts.
My favorite example of this: https://old.reddit.com/r/learnprogramming/comments/kegalv/my...
DotDict: A simple Python library to make chained attributes possible
31–40 of 55 posts
Re: DotDict: A simple Python library to make chained attributes possible
#32config = dd()
config.users.foo = 'bar' config.users = ‘baz’
Is config.users turned into a string and config.users.foo is lost? Or is the equal symbol mapped to something cleverer than assignment?
Re: DotDict: A simple Python library to make chained attributes possible
#33In case anyone is looking for mature alternatives that are used in production https://pypi.org/project/python-box/ https://pypi.org/project/munch/
Neither of these appear to have the chained attribute feature, from my surface level parse.
from box import Box
movie_box = Box({ "Robin Hood: Men in Tights": { "imdb stars": 6.7, "length": 104 } })
movie_box.Robin_Hood_Men_in_Tights.imdb_stars
from munch import Munch
b = Munch()
b.hello = 'world'
b.foo = Munch(lol=True)
b.foo.lolRe: DotDict: A simple Python library to make chained attributes possible
#34Re: DotDict: A simple Python library to make chained attributes possible
#35In case anyone is looking for mature alternatives that are used in production https://pypi.org/project/python-box/ https://pypi.org/project/munch/
Re: DotDict: A simple Python library to make chained attributes possible
#36My general rule is, if I have a dict with a fixed set of keys, it should be a dataclass. Problem solved! Then if I need to send or receive it, it's an easy conversion to a Pydantic BaseModel.
It's really the way to go, takes a minute or two to define and saves so much headaches in the long run.
Re: DotDict: A simple Python library to make chained attributes possible
#37My general rule is, if I have a dict with a fixed set of keys, it should be a dataclass. Problem solved! Then if I need to send or receive it, it's an easy conversion to a Pydantic BaseModel.
Re: DotDict: A simple Python library to make chained attributes possible
#38My general rule is, if I have a dict with a fixed set of keys, it should be a dataclass. Problem solved! Then if I need to send or receive it, it's an easy conversion to a Pydantic BaseModel.
Can dataclasses be serialized just as easily?
Or for JSON, dataclasses_json
Re: DotDict: A simple Python library to make chained attributes possible
#39Re: DotDict: A simple Python library to make chained attributes possible
#40In case anyone is looking for mature alternatives that are used in production https://pypi.org/project/python-box/ https://pypi.org/project/munch/
Neither of these appear to have the chained attribute feature, from my surface level parse.
>>> from box import Box
>>> empty_box = Box(default_box=True)
>>> empty_box.a.b.c.d.e.f.g #