Live data from Hacker News

DotDict: A simple Python library to make chained attributes possible

github.com

41–50 of 55 posts

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

#41

Earlier quoted context omitted.

Can dataclasses be serialized just as easily?

Yes via pickle. Or for JSON, dataclasses_json

Be aware that pickle is unsafe (as documented near the top of https://docs.python.org/3/library/pickle.html), so prefer other serialization formats except between trusted processes

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

#42
post #41

Earlier quoted context omitted.

Yes via pickle. Or for JSON, dataclasses_json

Be aware that pickle is unsafe (as documented near the top of https://docs.python.org/3/library/pickle.html ), so prefer other serialization formats except between trusted processes

I can't remember the last time I _wanted_ to pickle something. Parquet and JSON pretty much cover it for me. I guess pickling made more sense before tools like Pydantic were good and popular?

If I want all the python-specific nuance on disk, I know I'm in a bad place!

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

#43

Earlier quoted context omitted.

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.

I feel bad that no one seemed to get the joke. At the same time, that makes the whole thing funnier.

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

#44

Earlier quoted context omitted.

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

Chained means in your second example this would work without manually creating a new instance:

    b.foo.lol = True

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

#46
post #2

Good video on this subject: https://www.youtube.com/watch?v=nDd2WVb_Enw

Anthony’s video 100%.

For anyone who doesn’t watch… the point being made is that this has been done so many times that yet another implementation was pulled from the stdlib in 3.12 (AttrDict).

It’s true. I’ve implemented this several times myself, and thought it was a new/good idea every time until I remembered how many abstractions it breaks if I use it anywhere.

I can’t promise I won’t do it again in a few weeks! `NaturalDictLite` is probably coming around in the accompanying rotation of bad name ideas :p

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

#48

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?

Yep! Both msgspec (https://jcristharif.com/msgspec/supported-types.html#datacla...) and orjson support encoding dataclasses to JSON natively.

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

#49

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.

100%. I don’t understand the obsession with bending dicts into something they are inherently bad at when actual alternatives in dataclasses and Pydantic models right there.

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

#50

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?

In addition to the already-mentioned suggestions, you can also use marshmallow schemas to serialise complex objects, and there's even a package to autogen marshmallow schemas from dataclasses: https://pypi.org/project/marshmallow-dataclass/
Post reply on HN