#πŸ”’ Dataclasses: get child objects to structure into their own dataclasses?

18 messages Β· Page 1 of 1 (latest)

mighty lotus
#

Lets say I have a dataclass about a user. I get the data from its source as a nested dict:

{
name: 'test',
addresses: [{}, {}]
}

where addresses is a list of structured data, formattable into its own dataclass.

Can I put together something in the parent dataclass to automatically ingest the addresses and build them into their own defined dataclass? Or would I have to first turn them into instances of an Address dataclass, then pull that into a parent?

robust duneBOT
#

@mighty lotus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

simple monolith
hexed temple
#

Probably the latter?

simple monolith
#

Or cattrs or marshmallow

#

pydantic v2 etc etc

mighty lotus
#

I just came from a slack where I got a whole bunch of "ORMs suck!", and I think it's kinda muddled my mind

simple monolith
mighty lotus
#

Even with something like pydantic, I'm not aware of how to structure nested data, thus asking here

simple monolith
#

pydantic should just work with nested data

mighty lotus
#

Ok, I'll try again, maybe I made a mistake

#

And I'll look at sqlmodel... it may be just what I want, sqlalchemy-esque but less confusing (I spent the last 8 hours trying to read the sql alchemy docs and don't feel like I understand it yet)

#

Oh man, sqlmodel is based on sql alchemy? In 5 minutes, I think I have a better understanding of sqlalchemy than in the last 2 hours of documentation reading...

#

Ok, seems like trying to learn sqlalchemy and using pydantic is the way to go, and following sqlmodel as it matures more

#

Thanks for the help. Not sure I got an answer I can use, but meh, its one of those days

robust duneBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.