| 1 |
import datetime |
|---|
| 2 |
try: |
|---|
| 3 |
import fixedpoint |
|---|
| 4 |
except ImportError: |
|---|
| 5 |
fixedpoint = None |
|---|
| 6 |
|
|---|
| 7 |
try: |
|---|
| 8 |
import decimal |
|---|
| 9 |
except ImportError: |
|---|
| 10 |
decimal = None |
|---|
| 11 |
|
|---|
| 12 |
import dejavu |
|---|
| 13 |
from dejavu import Unit, UnitProperty, associate |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
class Zoo(Unit): |
|---|
| 17 |
Name = UnitProperty() |
|---|
| 18 |
Founded = UnitProperty(datetime.date) |
|---|
| 19 |
Opens = UnitProperty(datetime.time) |
|---|
| 20 |
LastEscape = UnitProperty(datetime.datetime) |
|---|
| 21 |
|
|---|
| 22 |
if fixedpoint: |
|---|
| 23 |
Admission = UnitProperty(fixedpoint.FixedPoint) |
|---|
| 24 |
else: |
|---|
| 25 |
Admission = UnitProperty(float) |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
class EscapeProperty(UnitProperty): |
|---|
| 29 |
def __set__(self, unit, value): |
|---|
| 30 |
UnitProperty.__set__(self, unit, value) |
|---|
| 31 |
z = unit.first(Zoo) |
|---|
| 32 |
if z: |
|---|
| 33 |
z.LastEscape = unit.LastEscape |
|---|
| 34 |
|
|---|
| 35 |
class Animal(Unit): |
|---|
| 36 |
Name = UnitProperty() |
|---|
| 37 |
ZooID = UnitProperty(int, index=True) |
|---|
| 38 |
Legs = UnitProperty(int) |
|---|
| 39 |
PreviousZoos = UnitProperty(list) |
|---|
| 40 |
LastEscape = EscapeProperty(datetime.datetime) |
|---|
| 41 |
Lifespan = UnitProperty(float, hints={'bytes': 4}) |
|---|
| 42 |
|
|---|
| 43 |
def in_first_zoo(self): |
|---|
| 44 |
return (self.PreviousZoo and self.PreviousZoo[0] == self.ZooID) |
|---|
| 45 |
|
|---|
| 46 |
associate(Zoo, 'ID', Animal, 'ZooID') |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
class Exhibit(Unit): |
|---|
| 50 |
|
|---|
| 51 |
Name = UnitProperty(str) |
|---|
| 52 |
ZooID = UnitProperty(int) |
|---|
| 53 |
Animals = UnitProperty(list) |
|---|
| 54 |
PettingAllowed = UnitProperty(bool) |
|---|
| 55 |
if decimal: |
|---|
| 56 |
Acreage = UnitProperty(decimal.Decimal) |
|---|
| 57 |
else: |
|---|
| 58 |
Acreage = UnitProperty(float) |
|---|
| 59 |
|
|---|
| 60 |
associate(Zoo, 'ID', Exhibit, 'ZooID') |
|---|
| 61 |
|
|---|
| 62 |
arena = dejavu.Arena() |
|---|
| 63 |
arena.register_all(globals()) |
|---|