[Schevo-devel] Re: [STATUS] - Current Status of Additions
Matthew Scott
mscott at goldenspud.com
Tue May 3 14:03:45 EDT 2005
Ilias Lazaridis wrote:
> A few weeks ago I've asked the project to decouple the navigator, thus I
> can contribute some changes.
Please feel free to contribute patches. Just because the code for
Schevo Navigator is still in the 'schevo.navigator' package doesn't
preclude us at all from accepting changes to it. Hack away :)
>
> Can you provide me with the [core] code to add:
>
> "Drivers to Cars"
>
> something like: Cars->AddDriver(<driver-instance>)
>
Let us assume we have an application "myapp" with the following entity
schema. Below the schema, I will paste in a Python shell session
working at the database level of Schevo.
The answer to your question about how to add a driver to a car is
expressed in this example by use of the Car, Driver, and Person entity
types and by the creation of Driver instances in the Python shell session.
class Car:
type = f.entity(allow=CarType)
vin = f.string()
_key(vin)
def __str__(self):
return '%(type)s :: %(vin)s' % self.sys.attrs
class CarType:
manufacturer = f.string()
model = f.string()
modelYear = f.integer()
_key(manufacturer, model, modelYear)
def __str__(self):
return '%(modelYear)i %(manufacturer)s %(model)s' % self.sys.attrs
class Driver:
car = f.entity(allow=CarType)
person = f.entity(allow=Person)
_key(car, person)
def __str__(self):
return '%(person)s :: %(car)s' % self.sys.attrs
class Person:
name = f.string()
def __str__(self):
return self.name
Here is the IPython session, started by using this command-line:
evo run --ipython myapp -- --no-navigator
If you wish, you could use the shell inside the navigator if you do not
want to install IPython on your system:
evo run myapp -- --navigator-shell
>>> # Initialization.
>>> tx = db.schemata.transaction
>>> # Create a car type.
>>> t = tx.Create('CarType',
... manufacturer='Ford', model='Taurus', modelYear=2000)
>>> carType = db.execute(t)
>>> carType
--- <CarType entity at 0x-49df7474 oid:1 rev:0>
>>> str(carType)
--- '2000 Ford Taurus'
>>> # Create a car.
>>> t = tx.Create('Car', type=carType, vin='199342JKLFDSJKL39')
>>> car = db.execute(t)
>>> car
--- <Car entity at 0x-49d4aaf4 oid:1 rev:0>
>>> str(car)
--- '2000 Ford Taurus :: 199342JKLFDSJKL39'
>>> # Create a couple of people who will drive the car.
>>> t = tx.Create('Person', name='Bob')
>>> personBob = db.execute(t)
>>> personBob
--- <Person entity at 0x-49df7514 oid:1 rev:0 name:'Bob'>
>>> str(personBob)
--- 'Bob'
>>> t = tx.Create('Person', name='Sally')
>>> personSally = db.execute(t)
>>> personSally
--- <Person entity at 0x-49df7a74 oid:2 rev:0 name:'Sally'>
>>> str(personSally)
--- 'Sally'
>>> # Let them drive the car.
>>> t = tx.Create('Driver', car=car, person=personBob)
>>> db.execute(t)
--- <Driver entity at 0x-49c5dc14 oid:1 rev:0>
>>> t = tx.Create('Driver', car=car, person=personSally)
>>> db.execute(t)
--- <Driver entity at 0x-49df70f4 oid:2 rev:0>
>>> # Give bob another car to drive.
>>> t = tx.Create('Car', type=carType, vin='12345')
>>> car2 = db.execute(t)
>>> t = tx.Create('Driver', car=car2, person=personBob)
>>> db.execute(t)
--- <Driver entity at 0x-49df7034 oid:3 rev:0>
>>> # Find which cars sally can drive.
>>> [(result, str(result)) for result
... in personSally.sys.links('Driver', 'person')]
---
[(<Driver entity at 0x-49df70f4 oid:2 rev:0>,
'Sally :: 2000 Ford Taurus :: 199342JKLFDSJKL39')]
>>> # Find which cars Bob can drive.
>>> [(result, str(result)) for result
... in personBob.sys.links('Driver', 'person')]
---
[(<Driver entity at 0x-49c5dc14 oid:1 rev:0>,
'Bob :: 2000 Ford Taurus :: 199342JKLFDSJKL39'),
(<Driver entity at 0x-49df7034 oid:3 rev:0>,
'Bob :: 2000 Ford Taurus :: 12345')]
>>> # Get the actual cars that bob can drive.
>>> [(driver.car, str(driver.car)) for driver
... in personBob.sys.links('Driver', 'person')]
---
[(<Car entity at 0x-49d4aaf4 oid:1 rev:0>,
'2000 Ford Taurus :: 199342JKLFDSJKL39'),
(<Car entity at 0x-49dec254 oid:2 rev:0>, '2000 Ford Taurus :: 12345')]
--
Matthew R. Scott
More information about the Schevo-devel
mailing list