示例#1
0
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} "
示例#2
0
class VO3(ValueObject):

    s1 = Attribute()
    s2 = Attribute()

    def __init__(self, s1='abc', s2='xyz'):
        self.s1 = s1
        self.s2 = s2
示例#3
0
class VO1(ValueObject):

    x = Attribute()
    y = Attribute()

    def __init__(self, x=1, y=2):
        self.x = x
        self.y = y
示例#4
0
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)
示例#5
0
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
示例#6
0
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
示例#7
0
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
示例#8
0
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
示例#9
0
class TestComp2(Component):
    """TestComp2"""

    attr1 = Attribute()
    attr2 = Attribute()

    def meth(self):
        pass

    attr3 = Attribute()

    @property
    def prop(self):
        pass
示例#10
0
class VO2(VO1):

    z = Attribute()

    def __init__(self, z=3):
        super(VO2, self).__init__()
        self.z = z
示例#11
0
class TestComp6(TestComp5):
    """TestComp6"""

    c = Attribute()

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
示例#12
0
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
示例#13
0
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)
示例#14
0
class TestComp5(TestComp4):
    """TestComp5"""

    a = Attribute()
    b = Attribute()
示例#15
0
class DummyEntity(Entity):

    id = Attribute(immutable=True)

    def __init__(self):
        self.id = id(self)
示例#16
0
class TestEntity2(Entity):

    id = Attribute(immutable=True, )

    def __init__(self, id):
        super().__init__(id)