def test_dice_constructor_with_loader(): import ruyaml # NOQA yaml = ruyaml.YAML(typ='unsafe', pure=True) ruyaml.add_constructor('!dice', dice_constructor, Loader=ruyaml.Loader) data = yaml.load('initial hit points: !dice 8d4') assert str(data) == "{'initial hit points': Dice(8,4)}"
def register_xxx(**kw): import ruyaml class XXX(ruyaml.comments.CommentedMap): @staticmethod def yaml_dump(dumper, data): return dumper.represent_mapping(u'!xxx', data) @classmethod def yaml_load(cls, constructor, node): data = cls() yield data constructor.construct_mapping(node, data) ruyaml.add_constructor( u'!xxx', XXX.yaml_load, constructor=ruyaml.RoundTripConstructor ) ruyaml.add_representer(XXX, XXX.yaml_dump, representer=ruyaml.RoundTripRepresenter)
def test_dice_constructor_with_loader(): import ruyaml # NOQA ruyaml.add_constructor(u'!dice', dice_constructor, Loader=ruyaml.Loader) data = ruyaml.load('initial hit points: !dice 8d4', Loader=ruyaml.Loader) assert str(data) == "{'initial hit points': Dice(8,4)}"
def _make_objects(): global MyLoader, MyDumper, MyTestClass1, MyTestClass2, MyTestClass3 global YAMLobject1, YAMLobject2, AnObject, AnInstance, AState, ACustomState global InitArgs, InitArgsWithState global NewArgs, NewArgsWithState, Reduce, ReduceWithState, MyInt, MyList, MyDict global FixedOffset, today, execute class MyLoader(ruyaml.Loader): pass class MyDumper(ruyaml.Dumper): pass class MyTestClass1: def __init__(self, x, y=0, z=0): self.x = x self.y = y self.z = z def __eq__(self, other): if isinstance(other, MyTestClass1): return self.__class__, self.__dict__ == other.__class__, other.__dict__ else: return False def construct1(constructor, node): mapping = constructor.construct_mapping(node) return MyTestClass1(**mapping) def represent1(representer, native): return representer.represent_mapping('!tag1', native.__dict__) ruyaml.add_constructor('!tag1', construct1, Loader=MyLoader) ruyaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper) class MyTestClass2(MyTestClass1, ruyaml.YAMLObject): ruyaml.loader = MyLoader ruyaml.dumper = MyDumper ruyaml.tag = '!tag2' def from_yaml(cls, constructor, node): x = constructor.construct_yaml_int(node) return cls(x=x) from_yaml = classmethod(from_yaml) def to_yaml(cls, representer, native): return representer.represent_scalar(cls.yaml_tag, str(native.x)) to_yaml = classmethod(to_yaml) class MyTestClass3(MyTestClass2): ruyaml.tag = '!tag3' def from_yaml(cls, constructor, node): mapping = constructor.construct_mapping(node) if '=' in mapping: x = mapping['='] del mapping['='] mapping['x'] = x return cls(**mapping) from_yaml = classmethod(from_yaml) def to_yaml(cls, representer, native): return representer.represent_mapping(cls.yaml_tag, native.__dict__) to_yaml = classmethod(to_yaml) class YAMLobject1(ruyaml.YAMLObject): ruyaml.loader = MyLoader ruyaml.dumper = MyDumper ruyaml.tag = '!foo' def __init__(self, my_parameter=None, my_another_parameter=None): self.my_parameter = my_parameter self.my_another_parameter = my_another_parameter def __eq__(self, other): if isinstance(other, YAMLobject1): return self.__class__, self.__dict__ == other.__class__, other.__dict__ else: return False class YAMLobject2(ruyaml.YAMLObject): ruyaml.loader = MyLoader ruyaml.dumper = MyDumper ruyaml.tag = '!bar' def __init__(self, foo=1, bar=2, baz=3): self.foo = foo self.bar = bar self.baz = baz def __getstate__(self): return {1: self.foo, 2: self.bar, 3: self.baz} def __setstate__(self, state): self.foo = state[1] self.bar = state[2] self.baz = state[3] def __eq__(self, other): if isinstance(other, YAMLobject2): return self.__class__, self.__dict__ == other.__class__, other.__dict__ else: return False class AnObject: def __new__(cls, foo=None, bar=None, baz=None): self = object.__new__(cls) self.foo = foo self.bar = bar self.baz = baz return self def __cmp__(self, other): return cmp( (type(self), self.foo, self.bar, self.baz), # NOQA (type(other), other.foo, other.bar, other.baz), ) def __eq__(self, other): return type(self) is type(other) and (self.foo, self.bar, self.baz) == ( other.foo, other.bar, other.baz, ) class AnInstance: def __init__(self, foo=None, bar=None, baz=None): self.foo = foo self.bar = bar self.baz = baz def __cmp__(self, other): return cmp( (type(self), self.foo, self.bar, self.baz), # NOQA (type(other), other.foo, other.bar, other.baz), ) def __eq__(self, other): return type(self) is type(other) and (self.foo, self.bar, self.baz) == ( other.foo, other.bar, other.baz, ) class AState(AnInstance): def __getstate__(self): return {'_foo': self.foo, '_bar': self.bar, '_baz': self.baz} def __setstate__(self, state): self.foo = state['_foo'] self.bar = state['_bar'] self.baz = state['_baz'] class ACustomState(AnInstance): def __getstate__(self): return (self.foo, self.bar, self.baz) def __setstate__(self, state): self.foo, self.bar, self.baz = state # class InitArgs(AnInstance): # def __getinitargs__(self): # return (self.foo, self.bar, self.baz) # def __getstate__(self): # return {} # class InitArgsWithState(AnInstance): # def __getinitargs__(self): # return (self.foo, self.bar) # def __getstate__(self): # return self.baz # def __setstate__(self, state): # self.baz = state class NewArgs(AnObject): def __getnewargs__(self): return (self.foo, self.bar, self.baz) def __getstate__(self): return {} class NewArgsWithState(AnObject): def __getnewargs__(self): return (self.foo, self.bar) def __getstate__(self): return self.baz def __setstate__(self, state): self.baz = state InitArgs = NewArgs InitArgsWithState = NewArgsWithState class Reduce(AnObject): def __reduce__(self): return self.__class__, (self.foo, self.bar, self.baz) class ReduceWithState(AnObject): def __reduce__(self): return self.__class__, (self.foo, self.bar), self.baz def __setstate__(self, state): self.baz = state class MyInt(int): def __eq__(self, other): return type(self) is type(other) and int(self) == int(other) class MyList(list): def __init__(self, n=1): self.extend([None] * n) def __eq__(self, other): return type(self) is type(other) and list(self) == list(other) class MyDict(dict): def __init__(self, n=1): for k in range(n): self[k] = None def __eq__(self, other): return type(self) is type(other) and dict(self) == dict(other) class FixedOffset(datetime.tzinfo): def __init__(self, offset, name): self.__offset = datetime.timedelta(minutes=offset) self.__name = name def utcoffset(self, dt): return self.__offset def tzname(self, dt): return self.__name def dst(self, dt): return datetime.timedelta(0) today = datetime.date.today()