SimplyKnownAsG / yamlize

Python YAML serializing library
Apache License 2.0
11 stars 5 forks source link

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

Closed padec closed 3 years ago

padec commented 3 years ago

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:

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

SimplyKnownAsG commented 3 years ago

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 commented 3 years ago

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