class Person(Object): first = Attribute(type=str) last = Attribute(type=str) full = Attribute(type=str)
class Animal(Object): name = Attribute(type=str) age = Attribute(type=int) def __init__(self, name, age): Object.__init__(self) self.name = name self.age = age
class PositivePoint(Object): x = Attribute(type=float) @x.validator def x(self, x): if x < 0.0: raise ValueError('Cannot set PositivePoint.x to {}'.format(x)) y = Attribute(type=float, validator=lambda self, y: y > 0)
class PositivePoint2(Object): x = Attribute(type=float) y = Attribute(type=float) @classmethod def from_yaml(cls, loader, node, round_trip_data=None): # from_yaml.__func__ is the unbound class method self = Object.from_yaml.__func__(PositivePoint2, loader, node, round_trip_data) if self.x < 0.0 or self.y < 0.0: raise YamlizingError( 'Point x and y values must be positive', node) return self
class DailyMenu(KeyedList): key_attr = MenuItem.name item_type = MenuItem day = Attribute(type=str) def __init__(self, day): self.day = day
class ConfigRoot(Object): my_int = Attribute(type=int) my_float = Attribute(type=float) my_str = Attribute(type=str) description = Attribute(type=str) required_defaults = Attribute(type=RequiredDefaults, default=RequiredDefaults()) job_batch = Attribute(type=JobBatch, default=None) job_streaming = Attribute(type=JobStreaming, default=None) elastic = Attribute(type=Elastic)
class AnimalWithFriends(Object): name = Attribute(type=str)
class ClassWithLists(Object): ints = Attribute(type=IntList, default=None)
class ClassWithLists(Object): ints = Attribute(type=IntList) strs = Attribute(type=StrList)
del cwl.ints self.assertEqual('{}\n', ClassWithLists.dump(cwl)) class AnimalWithFriends(Object): name = Attribute(type=str) class AnimalSequence(Sequence): item_type = AnimalWithFriends AnimalWithFriends.friends = Attribute(name='friends', type=AnimalSequence, default=None) class Test_two_way(unittest.TestCase): def test_IntList(self): self.assertEqual([1, 2, 3], IntList.load(IntList.dump([1, 2, 3]))) il = IntList((1, 2, 3)) self.assertEqual([1, 2, 3], il) with self.assertRaises(TypeError): IntList(123) def test_StrList(self): abc = 'a b c'.split() self.assertEqual(abc, StrList.load(StrList.dump(abc))) self.assertEqual(3, len(abc))
class MenuItem(Map): key_type = Typed(str) value_type = Dynamic name = Attribute(type=str)
class DailyMenu(KeyedList): key_attr = MenuItem.name item_type = MenuItem day = Attribute(type=str)
class NamedMap(Map): key_type = Typed(str) value_type = Dynamic name = Attribute(type=str)
class Owner(Object): name = Attribute(type=str) pets = Attribute(type=NamedKennel)
class Node(Object): host = Attribute(type=str, default=DEFAULT_ELASTIC_HOST) port = Attribute(type=int, default=DEFAULT_ELASTIC_PORT)
class JobStreaming(Object): kafka_broker = Attribute(type=str, default=DEFAULT_KAFKA_HOST)
class JobBatch(Object): redis_host = Attribute(type=str, default=DEFAULT_REDIS_HOST)
class RequiredDefaults(Object): x = Attribute(type=str, default="val-x") y = Attribute(type=str, default="val-y")
class Elastic(Object): nodes = Attribute(type=Nodes)