Type in and run the following code (also available as testentity.py
). It will use the refactored entity
module to define a MyEntity
class and work with some instances of this class. We will create, list, and update instances and even see an update fail because we try to assign a value that will not pass a validation for an attribute:
Chapter7/testentity.py
from entity import * class Entity(AbstractEntity): database="/tmp/abc.db" class MyEntity(Entity): a=Attribute(unique=True, notnull=True, affinity='float', displayname='Attribute A', validate=lambda x:x<5) a=MyEntity(a=3.14) print(MyEntity.list()) e=MyEntity.list(pattern=[('a',3.14)])[0] print(e) e.delete() a=MyEntity(a=2.71) print([str(e) for e in MyEntity.list()]) a.a=1 a.update() print([str(e) for e in MyEntity.list()]) a.a=9
The output produced by the print functions should look similar to the lines listed next, including the raised exception caused by an invalid...