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.

Support adding of representers for whole class-hierarchies.

See original GitHub issue

I have a project which has a bunch of Enums and must dump them safely to send the output to an application written in another language. So an output like:

!!python/object/apply:__main__.Foo [1]

is not viable. And adding representers is not maintainable as the given class hierarchy is strongly subject to modification (new classes are tacked on regularly, all subclasses of Enum). As it is now, I must register a new “representer” for each such class. As a contrived example:

from yaml import dump as dump_yaml, add_representer
from enum import Enum


class Foo(Enum):

    A = 1
    B = 2


class Bar(Enum):

    A = 1
    B = 2


def enum_representer(dumper, data):
    return dumper.represent_scalar('!enum', str(data.value))


data = {
    'value1': Foo.A,
    'value2': Bar.B
}

add_representer(Foo, enum_representer)
add_representer(Bar, enum_representer)
print(dump_yaml(data))

As you can see in the last couple of lines, I need to register a representer for each subclass of Enum. What makes things work in my case, is that those enums come from an external library in my application. If that library decides to add a new subclass and an instance of that ends up in a data structure I wan to serialise, the application will create a non-safe dump.

It would be much nicer if I could simply register a representer for the base class Enum.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
fyhertzcommented, Nov 27, 2020
class MyDumper(yaml.SafeDumper):
    def represent_data(self, data):
        if isinstance(data, Enum):
            return self.represent_data(data.value)
        return super().represent_data(data)

class Foo(Enum):
    A = 1
    B = 2

data = {
    'value1': Foo.A,
}

yaml.dump(data, Dumper=MyDumper)

Works for me!

0reactions
nitzmahonecommented, Dec 1, 2020

Adding a multi_representer for Enum (or some intermediate subclass) to a Dumper is the way the original API intends for this to work. Directly overriding represent_data on a custom dumper works too, but kinda sidesteps the designed mechanism.

Closing this, since there are several ways to handle this with the existing API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Characterizing osmolyte chemical class hierarchies and ...
These data support the conclusion that there is no strict chemical class hierarchy amongst the osmolytes with respect to protein thermal stabilization ...
Read more >
Classroom Popularity Hierarchy Predicts Prosocial and ... - NCBI
This study examined the coevolution of prosocial and aggressive popularity norms with popularity hierarchy (asymmetries in students' ...
Read more >
Designing Heterogeneous Class Hierarchies - MATLAB ...
Creating Classes That Support Heterogeneous Arrays. This topic describes the concepts involved in defining classes that support the formation of heterogeneous ...
Read more >
How the CLASS Dimensions and Domains Support One Another
Learn:• How the CLASS domains and dimensions work together• Why it's important to create a safe and supportive environment early in the ...
Read more >
About Classification - Trimble eCognition Suite
It is backwards compatible with eCognition 4 and older class hierarchies and can open them without major changes. The algorithm can be applied...
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