Let us first define for ourselves how we want to use an Entity
class, because the interface we create must match as closely as possible the things we would like to express in our code. The following example shows what we have in mind (available as carexample.py
):
Chapter5/carexample.py
from entity import Entity
class Car(Entity): pass
Car.threadinit('c:/tmp/cardatabase.db')
Car.inittable(make="",model="",licenseplate="unique")
mycar = Car(make="Volvo",model="C30",licenseplate="12-abc-3")
yourcar = Car(make="Renault",model="Twingo",licenseplate="ab-cd-12")
allcars = Car.list()
for id in allcars:
car=Car(id=id)
print(car.make, car.model, car.licenseplate)
The idea is to create a Car
class that is a subclass of Entity
. We therefore have to take the following steps: