1

How can ignore the member Trivial._ignore when serializing this object?

import yaml
class Trivial(yaml.YAMLObject):
    yaml_tag = u'!Trivial'
    def __init__(self):
        self.a = 1
        self.b = 2
        self._ignore = 3

t = Trivial()
print(yaml.dump(t))

prints

!Trivial
_ignore: 3
a: 1
b: 2
Sean Allred
  • 3,558
  • 3
  • 32
  • 71

1 Answers1

1
def my_yaml_dump(yaml_obj):
    my_ob = deepcopy(yaml_obj)
    for item in dir(my_ob):
        if item.startswith("_") and not item.startswith("__"):
             del my_ob.__dict__[item]
    return yaml.dump(my_ob)

something like this would ignore anything with a single (leading) underscore

I think this is what you want

class Trivial(yaml.YAMLObject):
    yaml_tag = u'!Trivial'
    def __init__(self):
        self.a = 1
        self.b = 2
        self._ignore = 3
    @classmethod
    def to_yaml(cls, dumper, data):
        # ...
        my_ob = deepcopy(data)
        for item in dir(my_ob):
            if item.startswith("_") and not item.startswith("__"):
                del my_ob.__dict__[item]
        return dumper.represent_yaml_object(cls.yaml_tag, my_ob, cls,
                                                flow_style=cls.yaml_flow_style)

although a better method (stylistically)

class SecretYamlObject(yaml.YAMLObject):
     hidden_fields = []
     @classmethod
     def to_yaml(cls,dumper,data):
         new_data = deepcopy(data)
         for item in cls.hidden_fields:
             del new_data.__dict__[item]
         return dumper.represent_yaml_object(cls.yaml_tag, new_data, cls,
                                            flow_style=cls.yaml_flow_style)

class Trivial(SecretYamlObject):
     hidden_fields = ["_ignore"]
     yaml_tag = u'!Trivial'
     def __init__(self):
        self.a = 1
        self.b = 2
        self._ignore = 3

print yaml.dump(Trivial())

this is adhering to pythons mantra of explicit is better than implicit

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • If you can edit this in such a way that a standard `yaml.dump(t)` will do 'the right thing', I'd be grateful `:)` I believe it would be a customization of [`YAMLObject.to_yaml`](http://pyyaml.org/wiki/PyYAMLDocumentation#YAMLObject), but I'm not sure how to redefine it. (No example is given unfortunately, and I'm not really familiar with serialization as a practice.) – Sean Allred Mar 31 '14 at 23:38
  • as an aside why are you using yaml? I think its less common than alot of other ways to serialize data ... – Joran Beasley Apr 01 '14 at 00:18
  • It's a capstone undergrad research project (the actual use case is algorithms; see GitHub/vermiculus/ssa-tool) – Sean Allred Apr 01 '14 at 00:32
  • This won’t work in all cases. I have a field holding a weakref to a database connection which pyyaml dump errors on. Guess what? So does deepcopy. – JL Peyret May 23 '19 at 21:34
  • you could try a shallow copy? your really just deleteing the keys from the copy which unless you have overridden `__del__` this should only decrement the refcount by one, leaving the original object on the original item unscathed – Joran Beasley May 24 '19 at 02:29