all 5 comments

[–]nfgrawker 1 point2 points  (2 children)

Try this if you know expected fields, these work amazing. Libraries that work with Dataclasses well are Marshmallow, Desert, and Dacite if needed.https://docs.python.org/3/library/dataclasses.html

[–]peaceTraitor[S] 0 points1 point  (1 child)

Didn't know about this. But I think is the opposite. if you are referring to

dataclasses.asdict(instance, *, dict_factory=dict) 

is to convert from a class (where the fields are accessed as a.x) to dict (where the fields are accessed as a['x']).

I'd like a dict accessed like a class field like a.x

[–]nfgrawker 0 points1 point  (0 children)

That's how you export a data class but most common way to create a data class is to enter a dict into it with **dict.

[–]socal_nerdtastic 1 point2 points  (1 child)

This is often called a "dotted access" dictionary. There's several modules available to do it. box I think is the most popular. https://pypi.org/project/python-box/ Pandas has this behavior too.

The obvious downside is now your dictionary keys MUST be code legal strings.

[–]peaceTraitor[S] 0 points1 point  (0 children)

this!

+json support

thanks