class Car(Entity): id = UniqueIdAttribute() make = Attribute(immutable=True) model = Attribute(immutable=True) wheels = QualifiedMultiValueAttribute(WheelPosition, default={}) extras = MultiValueAttribute(default=set()) registered = Attribute(default=False) def __init__(self, make: str, model: str, *, listener: StateChangedListener = None): super().__init__() if listener: # create notifyer and add listener StateChangedNotifyerExtension(self).add_listener(listener) self.make = make self.model = model def change_tire(self, wheel_pos: WheelPosition, tire: Tire) -> None: try: wheel = self.wheels[wheel_pos] except KeyError: raise ValueError(f"No {wheel_pos} wheel") from None wheel.tire = tire # setting attribute of nested entity doesn't trigger change # notification, so it has to be done explicitely self.state_changed() def __repr__(self): """repr(self)""" return f"<{self.__class__.__name__}: {self.id} "
class VO3(ValueObject): s1 = Attribute() s2 = Attribute() def __init__(self, s1='abc', s2='xyz'): self.s1 = s1 self.s2 = s2
class VO1(ValueObject): x = Attribute() y = Attribute() def __init__(self, x=1, y=2): self.x = x self.y = y
class VO5(ValueObject): v1 = Attribute() v4 = Attribute() a = Attribute() def __init__(self, a, x=7, s5=9): self.a = a self.v1 = VO1(x=x) self.v4 = VO4(s5=s5)
class Tire(Entity): id = UniqueIdAttribute() make = Attribute(immutable=True) size = Attribute(immutable=True) def __init__(self, make: str, size: str) -> None: super().__init__() self.make = make self.size = size
class VO4(VO3): s3 = Attribute() s4 = Attribute() s5 = Attribute() def __init__(self, s3=1, s4=2, s5=3): super(VO4, self).__init__() self.s3 = s3 self.s4 = s4 self.s5 = s5
class Wheel(Entity): id = UniqueIdAttribute() type_of_rim = Attribute(immutable=True) tire = Attribute(default=None) def __init__(self, type_of_rim: RimType, tire: Optional[Tire] = None) \ -> None: super().__init__() self.type_of_rim = type_of_rim self.tire = tire
class Person(Entity): id = Attribute(immutable=True) first_name = Attribute() last_name = Attribute() date_of_birth = Attribute() def __init__(self, first_name, last_name, date_of_birth): self.id = str(hash((first_name, last_name, date_of_birth))) self.first_name = first_name self.last_name = last_name self.date_of_birth = date_of_birth
class TestComp2(Component): """TestComp2""" attr1 = Attribute() attr2 = Attribute() def meth(self): pass attr3 = Attribute() @property def prop(self): pass
class VO2(VO1): z = Attribute() def __init__(self, z=3): super(VO2, self).__init__() self.z = z
class TestComp6(TestComp5): """TestComp6""" c = Attribute() def __init__(self, a, b, c): self.a = a self.b = b self.c = c
class Prospect(Person): interests = Attribute() def __init__(self, first_name, last_name, date_of_birth, interests='not known yet'): super().__init__(first_name, last_name, date_of_birth) self.interests = interests
class Comp1(Component): x = Attribute() def __init__(self, x): self.x = x def __setattr__(self, name, value): # noqa: D103 super().__setattr__(name, value) try: notifyer = StateChangedNotifyer[self] except ValueError: pass else: notifyer.notify_state_changed(self)
class TestComp5(TestComp4): """TestComp5""" a = Attribute() b = Attribute()
class DummyEntity(Entity): id = Attribute(immutable=True) def __init__(self): self.id = id(self)
class TestEntity2(Entity): id = Attribute(immutable=True, ) def __init__(self, id): super().__init__(id)