Live data from Hacker News

DotDict: A simple Python library to make chained attributes possible

github.com

31–40 of 55 posts

Re: DotDict: A simple Python library to make chained attributes possible

#31

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...

That is a work of art, and also you have to appreciate the author’s commitment to the bit.

Re: DotDict: A simple Python library to make chained attributes possible

#33

In 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.

It's there on the readme/pypi home for both; unless i'm misunderstanding what you mean by "chained"

    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.lol

Re: DotDict: A simple Python library to make chained attributes possible

#34
post #24
post #23

edict() is an interesting name for a dictionary function.

It’s a class instantiation with no arguments

Yeah, I was referring to the name choice. Edict being “A decree or proclamation issued by an authority and having the force of law.”

Re: DotDict: A simple Python library to make chained attributes possible

#36

My 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.

And the dataclass serves as documentation for the available attributes and type safety..

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

#37

My 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?

Re: DotDict: A simple Python library to make chained attributes possible

#38

My 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?

Yes via pickle.

Or for JSON, dataclasses_json

Re: DotDict: A simple Python library to make chained attributes possible

#40

In 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.

python-box does this in the most intuitive way possible:

    >>> from box import Box
    >>> empty_box = Box(default_box=True)
    >>> empty_box.a.b.c.d.e.f.g # 
Post reply on HN