yaml.dump writes entire object, not just what's included in __repr__()
See original GitHub issueSay, I started with a short test program and trying the Monster class in the doc:
class Monster(yaml.YAMLObject):
yaml_tag = u'!Monster'
def __init__(self, name, hp, ac, attacks):
self.name = name
self.hp = hp
self.ac = ac
self.attacks = attacks
def __repr__(self):
return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % (
self.__class__.__name__, self.name, self.hp, self.ac, self.attacks)
It worked fine at the beginning; but, when I started to enhance the class and include additional attributes, they all showed up in the YAML file…and, I would very much like to dictate what’s get written out…which I thought that was the purpose of the repr method.
How to only dump out hand-picked attributes?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
pyyaml: dumping without tags - python - Stack Overflow
This seems to make dumping unicode objects work the same as dumping str objects for me (Python 2.6). In [72]: yaml.dump(u'abc') Out[72]: 'abc\n ......
Read more >PyYAML Documentation
PyYAML is a YAML parser and emitter for Python. Installation. Simple install: pip install pyyaml. To install from source, download the source package...
Read more >A Powerful Python Trick: Custom YAML tags & PyYAML
A simple yet powerful way to improve the way you write and use configuration files in Python.
Read more >YAML: The Missing Battery in Python
Read and write YAML documents in Python; Serialize Python's built-in and custom data types to YAML; Safely read YAML documents from untrusted ...
Read more >Python YAML – Read, Write, Parse YAML - PYnative
As you know, when the application processes lots of information, It needs to take a data dump. Using dump(), we can translate Python...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ha, nice! That was simple enought; got it working. Thanks.
__repr__
is mainly about customizing how Python displays an object for human consumption when it’s printed, so it returns a string of whatever format you want (the Type(key1=value1, key2=value2) thing is a convention, you could return any string you want).__getstate__
is about dumping structured data for machine consumption (which is what pyyaml’s dumper needs).The default behavior without that method defined just uses the dictionary returned by the internal
__dict__
attribute that stores the attribute contents of a typical Python object. In this case, if you define a__getstate__
method to return a customized dictionary with only the stuff you want, it should have the desired effect, so long as you’re not also expecting to be able to round-trip these objects through some other serializer that relies on__getstate__
(as whatever state you omit from your dictionary will be lost in the translation).