SimplyKnownAsG / yamlize

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

Problem wanting to customize YAML writing #10

Open john-science opened 3 years ago

john-science commented 3 years ago

So, last week I was working on an armi ticket, and came up with a real need to hide data inside a yamlize.object, that wouldn't be written out to the final YAML. This should have been a small tweak, but ended up being rather hard to trick yamlize to do.

The solutions we came up with were:

  1. Add an Attribute to the yamlize.object and delete it right before we .dump().
  2. Add a hidden class attribute and use Python hasattr to look for it.

So, we had a solution eventually, but we lost a lot of time on it. What would have really helped would be if Yamlize:

  1. Offered a way to make an Attribute "hidden", meaning it would never get written to a YAML.
  2. Offered a way to subclass Attribute, and have that subclass be used throughout the YAML file.

I suppose (1) above would be easier, but (2) would provide a lot more flexibility in the future.

I'd be happy to help implement a solution, if you that's amenable to you.

SimplyKnownAsG commented 3 years ago

Oh man, that's a bummer. It's been a while, but I threw together an example that I think works...

class Animal(Object):

    name = Attribute()
    age = Attribute()

    def __init__(self, name, age):
        self.name = name
        self.age = age

# the subclass is serializable; however, it is not a direct subclass of Object, etc.
class Animal2(Animal):

    def __init__(self, name, age):
        Animal.__init__(self, name, age)
        self.lossy = 'will not be written'
SimplyKnownAsG commented 3 years ago

More importantly, this is a good feature to add. Please look into it if/when you can :-)! I believe Attribute, or at least _Attribute is inheritable. If you need help, or have questions, feel free to reach out. If you need to air grievances of the complexity involved in hacking Python system to do what yamlize does.... please refactor. There's either too many or too few layers of abstraction.