Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is Yamlize adapted to serialize a list of objects with different classes ? #9

Closed
padec opened this issue Apr 8, 2021 · 2 comments
Closed

Comments

@padec
Copy link

padec commented Apr 8, 2021

Hello,
This package looks great but i'm not seeing how to use it for my concern.
My application allows to define a scenario that is made of nested Python objects.
The root object (name it Root) contains some attributes and a list of objects (name them LeafLevel1Type1, LeafLevel1Type2,...),
and each of these Level1 objects is made also of some attributes and a list of objects (name them LeafLevel2Type1, LeafLevel2Type2,...).
To summarize, it is a tree. Each leaf has attributes and a list of other objects that are themselves leaves.
I need to store the scenario (and obviously to be able to load it) using a yaml serialization.
Moreover, only a part of the object's attributes, has to be saved in the file.
I don't see how to use yamlize on 2 subjects:

  • My objects in the lists are instances of different classes and what i understand is that you need to specify the object type of the objects belonging to a Sequence (or Maps or KeyedList). (I refer in the documentation for example class People(Sequence):
    ... item_type = Person ).
    So i don't know how to map my lists of heterogeneous objects with yamlize.

  • When using a yamlize.Sequence. The object cannot have attributes. My objects are made of attribute and one of them is a list of other objects (for me a list seems to be implemented as a Sequence).
    So it seems that i can't use Sequence in my objects.

Am i wrong in my analyze ?
Can you please give hints on how to implement my tree serialization with yamlize ?
Thanks in advance for your support.
BRs

@padec padec changed the title Is Yamlize adapted to serialize a list of objects with different classes. Is Yamlize adapted to serialize a list of objects with different classes ? Apr 9, 2021
@SimplyKnownAsG
Copy link
Owner

SimplyKnownAsG commented Jul 22, 2021

Wo! sorry for the late response. Evidently, I don't have email set up or something.... I'm guessing you've moved on at this point :-(.

My objects in the lists are instances of different classes

I'd need to know more about how related the objects are to know. There are two examples in the README about subclassing. The Shapes example shows how you can use a base class's name to infer the type and then construct it upon loading. Now that I look at the example, it may be missing a constructor that assigns the shape str attribute ... whoops.

When using a yamlize.Sequence. The object cannot have attributes.

Correct. This is really a function of YAML itself. How would YAML represent an object and it's items?

first: "First name:"
last: "Last name."
friends: [...]

You could, however, define your own @classemthod to_yaml and from_yaml methods.

@SimplyKnownAsG
Copy link
Owner

Haven't seen a response on this, closing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants