Skip to content

supercollections prevents dumping back as yaml #258

@kratsg

Description

@kratsg

a simple usecase might be to call yaml.safe_dump back on any variables loaded with this plugin, but the lack of any encoders causes a crash. A dump has the custom objects built in:

(Pdb) yaml.dump(contribution["notes"])
'!!python/object/new:super_collections.SuperList\nlistitems:\n- MET/sqrt(Ht) signal shape\n- !!python/object/new:super_collections.SuperList\n  listitems:\n  - why are smaller mass splittings harder? Is this due to chargino decay products?\n  - Check how MET and Ht differ between signals\n'

as opposed to the crash with safe_dump

(Pdb) yaml.safe_dump(contribution["notes"])
*** yaml.representer.RepresenterError: ('cannot represent an object', ['MET/sqrt(Ht) signal shape', ['why are smaller mass splittings harder? Is this due to chargino decay products?', 'Check how MET and Ht differ between signals']])

It would be really nice if the mkdocs-macros didn't just give SuperDict/SuperLists which makes things a bit harder, and somewhat unexpected when one is just providing yaml metadata in the frontmatter. The easiest solution I find is this

import yaml
import super_collections

def custom_representer(dumper, data):
    """Convert custom objects into YAML serializable format."""
    if isinstance(data, super_collections.SuperList):
        return dumper.represent_list(data)
    elif isinstance(data, super_collections.SuperDict):
        return dumper.represent_dict(data)
    else:
        return dumper.represent_str(str(data))

# Register the custom representer for unknown objects
yaml.add_multi_representer(object, custom_representer)

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnnoyanceSomething annoying (not a bug)fixedA fix has been submittedkeep in mindInteresting idea, keep in mind for the future

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions