Pythonistas, any recommendation of serialization library to convert objects to dicts an vice-versa? I've used Marshmallow for many years, but wondering if there is something else out there that I am missing.
(Not to/from JSON, but dicts)
[#]Python #serialization
=> More informations about this toot | More toots from sergi@floss.social
@sergi vars()? 😛
But seriously though, any particular features you're looking for? Not that I am any sort of expert on serialization libraries but I might know about something....
[#]Python
=> More informations about this toot | More toots from diazona@techhub.social
@diazona I want to implement the repository pattern with pymongo, which consumes and returns dicts, so I need a way to convert between my domain classes and dicts.
=> More informations about this toot | More toots from sergi@floss.social
@sergi I see, well, it sounds like something simple could be suitable?
One option that comes to mind off the top of my head is dataclasses. If you make your domain classes into dataclasses with the required fields then you get dict conversion pretty much for free. It might not be as slick as with a library, but I think it gets nearly as close as you can get without having to define custom logic to convert between your class's fields and the dict's fields. You could perhaps also looks at the attrs library, which I think is a more featurful version of dataclasses, but I haven't used it myself so I don't really know how it'd work for you.
=> More informations about this toot | More toots from diazona@techhub.social
@sergi I've just used cattrs in a recent project and rather liked it. You create an attrs model/object with the fields you want to serialize... using the cattrs converter, you can structure/un-structure pretty easily. Not sure if that's what you need, but worth a look if you haven't already.
https://catt.rs/en/stable/
=> More informations about this toot | More toots from pythonbynight@fosstodon.org
@pythonbynight I had completely forgotten about cattrs! I need to look deeper into it, but it seems a good contender against Marshmallow.
=> More informations about this toot | More toots from sergi@floss.social
@sergi
Pydantic's model class has methods to populate an instance from attributes on an arbitrary object, and to go from a model instance to a dict for serialization. That would require defining the model's fields, though.
I don't remember for sure, but it might also have a way to create a model definition from reflection, which would get you the rest of the way...
=> More informations about this toot | More toots from cazabon@mindly.social
@cazabon I think that Pydantic would be overkill for what I'm trying to achieve. I see Pydantic as a validation library, and I don't care about validation but for my case (I have explained it in another post of this thread). But thanks for the suggestion!
=> More informations about this toot | More toots from sergi@floss.social This content has been proxied by September (3851b).Proxy Information
text/gemini