Created
January 4, 2020 19:54
-
-
Save kenmanheimer/1326236579c36647fb91d90a2b3fa7d0 to your computer and use it in GitHub Desktop.
Scratch pyyaml for custom object export/import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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