News for hackers. Stuff that matters.
Re:DotDict: A simple Python library to make chained attribut... by claytonjy on Wednesday August 02, 2023 @07:05PM (#36979088)
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.

Re:DotDict: A simple Python library to make chained attribut... by smashed on Wednesday August 02, 2023 @07:29PM (#36979333)
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 attribut... by behnamoh on Wednesday August 02, 2023 @07:30PM (#36979337)
Can dataclasses be serialized just as easily?
Re:DotDict: A simple Python library to make chained attribut... by paulddraper on Wednesday August 02, 2023 @07:32PM (#36979348)
Yes via pickle.

Or for JSON, dataclasses_json

Re:DotDict: A simple Python library to make chained attribut... by dharmab on Wednesday August 02, 2023 @07:42PM (#36979438)
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 attribut... by claytonjy on Wednesday August 02, 2023 @07:59PM (#36979553)
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 attribut... by nurettin on Sunday August 06, 2023 @12:52AM (#37019081)
I use pickle to serialize RPC requests between internal services. Nothing beats it in terms of speed and ease of use.
Re:DotDict: A simple Python library to make chained attribut... by jammycrisp on Thursday August 03, 2023 @12:12AM (#36981274)
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 attribut... by jwestbury on Thursday August 03, 2023 @05:00AM (#36983210)
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/
Re:DotDict: A simple Python library to make chained attribut... by Fatal1ty on Thursday August 03, 2023 @06:15PM (#36992785)
Yes, there are many tools for converting dataclasses to dict, json or something else, mashumaro (https://github.com/Fatal1ty/mashumaro) is one of them.
Re:DotDict: A simple Python library to make chained attribut... by rivepica on Thursday August 03, 2023 @01:15AM (#36981669)
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.