Ancestors

Toot

Written by Sergi on 2024-10-22 at 21:04

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

Descendants

Written by David Zaslavsky on 2024-10-22 at 21:56

@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

Written by Sergi on 2024-10-22 at 23:06

@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

Written by David Zaslavsky on 2024-10-22 at 23:18

@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

Written by Mario Munoz on 2024-10-22 at 22:03

@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

Written by Sergi on 2024-10-22 at 23:05

@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

Written by C. on 2024-10-23 at 00:00

@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

Written by Sergi on 2024-10-23 at 05:31

@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

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113353101419216634
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
351.919858 milliseconds
Gemini-to-HTML Time
1.568841 milliseconds

This content has been proxied by September (3851b).