def test_frozenset_roundtrip(): # By default, frozensets become sets value = frozenset((4, 3, 2)) camel = Camel() dumped = camel.dump(value) # TODO this should use ? notation assert dumped == "!!set\n2: null\n3: null\n4: null\n" assert camel.load(dumped) == set(value)
def test_python_roundtrip(value, expected_serialization): camel = Camel([PYTHON_TYPES]) dumped = camel.dump(value) assert dumped == expected_serialization # Should be able to load them without the python types vanilla_camel = Camel() assert vanilla_camel.load(dumped) == value
def test_tuple_roundtrip(): # By default, tuples become lists value = (4, 3, 2) camel = Camel() dumped = camel.dump(value) # TODO short list like this should be flow style? assert dumped == "- 4\n- 3\n- 2\n" assert camel.load(dumped) == list(value)
def test_docs_table_v2(): # Tables can be rectangles now! class Table(object): def __init__(self, height, width): self.height = height self.width = width def __repr__(self): return "<Table {self.height!r}x{self.width!r}>".format(self=self) from camel import Camel, CamelRegistry my_types = CamelRegistry() @my_types.dumper(Table, 'table', version=2) def _dump_table_v2(table): return { 'height': table.height, 'width': table.width, } @my_types.loader('table', version=2) def _load_table_v2(data, version): return Table(data["height"], data["width"]) @my_types.loader('table', version=1) def _load_table_v1(data, version): edge = data["size"]**0.5 return Table(edge, edge) table = Table(7, 10) assert Camel([my_types]).dump(table) == textwrap.dedent(""" !table;2 height: 7 width: 10 """).lstrip() @my_types.dumper(Table, 'table', version=1) def _dump_table_v1(table): return { # not really, but the best we can manage 'size': table.height * table.width, } camel = Camel([my_types]) camel.lock_version(Table, 1) assert camel.dump(Table(5, 7)) == "!table;1\nsize: 35\n"
def test_docs_table_v2(): # Tables can be rectangles now! class Table(object): def __init__(self, height, width): self.height = height self.width = width def __repr__(self): return "<Table {self.height!r}x{self.width!r}>".format(self=self) from camel import Camel, CamelRegistry my_types = CamelRegistry() @my_types.dumper(Table, 'table', version=2) def _dump_table_v2(table): return { 'height': table.height, 'width': table.width, } @my_types.loader('table', version=2) def _load_table_v2(data, version): return Table(data["height"], data["width"]) @my_types.loader('table', version=1) def _load_table_v1(data, version): edge = data["size"] ** 0.5 return Table(edge, edge) table = Table(7, 10) assert Camel([my_types]).dump(table) == textwrap.dedent(""" !table;2 height: 7 width: 10 """).lstrip() @my_types.dumper(Table, 'table', version=1) def _dump_table_v1(table): return { # not really, but the best we can manage 'size': table.height * table.width, } camel = Camel([my_types]) camel.lock_version(Table, 1) assert camel.dump(Table(5, 7)) == "!table;1\nsize: 35\n"
def test_basic_roundtrip(value, expected_serialization): camel = Camel() dumped = camel.dump(value) assert dumped == expected_serialization assert camel.load(dumped) == value
def test_dieroll(): value = DieRoll(3, 6) camel = Camel([reg]) dumped = camel.dump(value) assert dumped == '!roll 3d6\n...\n' assert camel.load(dumped) == value
def save_yaml(data, path, lock_versions={}): with open(path, 'w') as f: c = Camel([standard_types_registry, camel_registry]) for klass, version in lock_versions.items(): c.lock_version(klass, version) f.write(c.dump(data))
if filename is None: # this is a fresh duck! we want an image of it at the start point image = duck.make_image() advancement = duck.advance(response) if advancement is not None: for string in advancement: if ( duck.scenario is None and duck.success is None and image is None ): twitter.update_status( string, in_reply_to_status_id=latest_tweet.id if latest_tweet else None ) else: if image is None: image = duck.make_image() image.save(DUCK_IMAGE_LOCATION) twitter.update_with_media(DUCK_IMAGE_LOCATION, string) if filename is None: filename = '{}.yaml'.format(now().isoformat()) with open(os.path.join(DUCK_DIR, filename), 'w') as f: f.write(camel.dump(duck))
def test_dieroll2(): value = DieRoll(3, 6) camel = Camel([reg2]) dumped = camel.dump(value) assert dumped == '!roll\nnumdice: 3\nfaces: 6\n' assert camel.load(dumped) == value