question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yaml.dump writes entire object, not just what's included in __repr__()

See original GitHub issue

Say, 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gsalcommented, Feb 2, 2022

Ha, nice! That was simple enought; got it working. Thanks.

0reactions
nitzmahonecommented, Feb 2, 2022

__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).

def __getstate__(self):
    return dict(label_x=self.axlabel_x, label_y=self.axlabel_y)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found