Skip to content

Instantly share code, notes, and snippets.

@kenmanheimer
Created January 4, 2020 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmanheimer/1326236579c36647fb91d90a2b3fa7d0 to your computer and use it in GitHub Desktop.
Save kenmanheimer/1326236579c36647fb91d90a2b3fa7d0 to your computer and use it in GitHub Desktop.
Scratch pyyaml for custom object export/import
def minimal_representer(dumper, item):
# type: (yaml.representer.Representer, spherical.item.Item) -> str
"""Minimal example of a representer that has features items will need."""
tag = "!spherical.example"
for i in spherical.utils.partially_sorted_partition(item):
fields = [['id', str(item._id)],
['title', item._title]]
return dumper.represent_sequence(tag, fields) # type: str
def minimal_constuctor(loader, # type: yaml.constructor.Constructor
node # type: yaml.nodes.Node
):
# type: (...) -> spherical.item.Item
seq = loader.construct_sequence(node)
# Problem - resulting seq lacks data: [[], []]
return seq
yaml.add_representer(spherical.item.Item, minimal_representer)
yaml.add_representer(Workspace, minimal_representer)
yaml.add_constructor("!spherical.example", minimal_constuctor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment