/test/front_page.py
Python | 148 lines | 88 code | 49 blank | 11 comment | 36 complexity | 5478eef96b98ea6f83a1dcf1172b7289 MD5 | raw file
1import sys 2sys.path.append(r'C:\eclipse_main\workspace\POD_ROOT\trunk') 3import pod 4 5class Person(pod.Object): # Any instances created by a class that descends from pod.Object 6 pass # will automatically persist. 7 8class Tycoon(Person): # Now, add a class Tycoon that descends from Person . . . 9 10 def __init__(self, **kwargs): 11 12 pod.Object.__init__(self, **kwargs) 13 14 self.yachts = [] # Also note, attributes can be of any type that can be pickled 15 self.villas = {} # including lists, sets, and dictionaries. Note, these are native 16 # Python collections and are not scalable but are fine for small collections. 17 18 self.workers = pod.Set() # If you want a scalable collection implemented using SQL, use the 19 # pod.List, pod.Dict, or pod.Set collection objects. 20 21 22class WorkerBee(Person): 23 24 def __init__(self, boss, **kwargs): 25 Person.__init__(self, **kwargs) 26 self.boss = boss # Pointer to pod object, much like foreign key. 27 self.boss.workers.add(self) # This pod.Set collection acts as one-to-many relation 28 29 30 def pre_delete(self): # In pod, nothing is done 'automatically' for you. You have to 31 self.boss.workers.remove(self) # take care of cleanup yourself 32 33 34class Yacht(pod.Object): # Let's add another class that descends directly from pod.Object. 35 36 name = pod.typed.String(index = False) # Here, we type the attributes in order to allow faster raw SQL queries. 37 length = pod.typed.Float(index = True) # You don't need to do this -- it just makes insertion/querying faster. 38 awards = pod.typed.Object(index = True) # Add an SQL index to make it faster at expense of slower insert. 39 owner = pod.typed.PodObject(index = True) # You can even make an attribute of type typed.PodObject which acts much 40 # like a 'foreign id' column -- except it will accept any type of 41 # pod.Object instance. 42 def __init__(self, owner, **kwargs): 43 pod.Object.__init__(self, **kwargs) 44 self.owner = owner 45 self.owner.yachts.append(self) 46 self.photos = [] # pod Objects can have a mix of dynamic and typed attributes. 47 48 49db = pod.Db(file = 'mypod.sqlite3', index = True, remove = True) # Connect before making any instances . . . 50 # By setting the index to 'True' an SQL index is created for 51 # all untyped 'dynamic' attributes enabling fast queries. 52 53 54hsing = Person(name = 'Hsing', some_attr = 'foo') # Now, add some Person, Tycoon, and WorkerBee objects to 55helen = Person(name = 'Helen', some_other_attr = ['bar', 'baz']) # the database . . . 56 57don = Tycoon(name = 'Don', age = 63, catch_phrase = "You're fired!") 58george = Tycoon(name = 'George', age = 61, catch_phrase = "I guarantee it") 59 60bruce = WorkerBee(name = 'Bruce', age = 40, boss = don, random_attr = 10.23) 61azhar = WorkerBee(name = 'Azhar', age = 40, boss = george, random_attr = {'key': ['value', (1, 2, 3)]}) 62 63Yacht(owner = george, name = 'The Trickle Downer', length = 40.5) 64Yacht(owner = george, name = 'The TARP Runner', length = 42.1, some_random_attr = 'foo') 65 66db.commit() 67 68 69for peep in Person: # Note that any pod.Object class is itself an iterator . . . 70 print peep.name # Prints 'Hsing', 'Helen', 'Don', 'George', 'Bruce', 'Azhar' 71 72for peep in Person: # Note, instances do not need to all have the same attributes. 73 try: 74 print peep.some_attr # Prints 'foo', then throws KeyError 75 except: 76 print getattr(peep, 'some_attr', None) # Prints None, None, None, None, None 77 78for peep in [peep for peep in Person if peep.name[0] == 'H']: # You can 'query' the database using list comprehensions. 79 print peep.name # Prints 'Hsing', 'Helen' 80 81 # Just note, every object loaded from db and compared in Python. 82 83for peep in Person.where.name[0] == 'H': # To query more efficently using SQL, use pod query syntax. 84 print peep.name # Prints 'Hsing', 'Helen' 85 86for peep in Person.where.random_attr == {'key': ['value', (1, 2, 3)]}: # You can query with == and != on any type of object. 87 print peep.name # Print 'Azhar' 88 89for peep in Person.where.age > 62: # '=', '!=', '<', '>', '<=', '>=' are very fast if you've defined an index 90 print peep.name, peep.age # Prints 'Don', 63 91 92for peep in Person.where.some_other_attr: # This just gets all peeps that have an attribute 'some_other_attr' 93 print peep.some_other_attr # Prints ['bar', 'baz'] 94 95for peep in (Person.where.age > 62) & (Person.where.age < 64): # You can also chain together query conditionals 96 print peep.name, peep.age # Prints 'Don', 63 97 98don = (Tycoon.where.name == 'Don').get_one() # Here, we show that pod maintains a single object reference. 99bruce = (WorkerBee.where.name == 'Bruce').get_one() # In essence, pod recreates your original memory space. 100 101print don is bruce.boss # Prints True. 102 103for bee in don.workers: # You can iterated through pod collections just like native 104 bee.fired = True # Python collections 105 106for bee in WorkerBee.where.boss == don: # Or, if you like, use a relational database technique -- you choose. 107 bee.fired = True 108 109for yacht in Yacht.where.name == 'The Trickle Downer': # The query syntax using typed attributes is exactly the same. 110 print yacht.owner.name # Prints 'George' 111 112for yacht in Yacht.name == 'The Trickle Downer': # However, with typed attributes you can drop the 'where' if you want. 113 print yacht.owner.name # Prints 'George' 114 115for yacht in Yacht.owner == george: # Using a typed attribute and a relational structure is faster than 116 # using a pod collection like pod.Set (but also less powerful/flexible). 117 print yacht.owner.name # Prints 'George', 'George' 118 119query = pod.Query(select = Yacht.name | Yacht.length, # Or, for full SQL control, use a query object. 120 where = (Yacht.length < 41) | (Yacht.length == 42.1), # Conditionals are chained together with '|' or '&'. 121 order_by = Yacht.length.desc(), 122 limit = 2) 123 124for yacht in query: # Now iterate on the query . . . 125 126 print yacht.length # Prints 42.1, 40.5 127 128 if yacht.length < 41: # Just like regular Python objects, you can add attributes 129 yacht.another_random_attr = ['foo', 'bar', 'baz'] # on the fly . . . 130 131 132hsing = (Person.where.name == 'Hsing').get_one() # Also, any pod.Object instance implements the dictionary interface. 133 134for key,value in hsing.iteritems(id = False): 135 print key,value # Prints 'name', 'Hsing' and 'some_attr', 'foo' 136 137db.store['some_list'] = [10, 20, 30] # Note, Each 'store' is seperate from all others. 138db.store.another_list = [1, 1, 2 ] # 139Person.store['some_list'] = [40, 50, 60] # Also, you can use dictionary '[ ]' notation 140Tycoon.store.main_tycoon = george # or object '.' notation. 141 142db.commit() # Commit changes to the 'stores' to database. 143 144for yacht in Tycoon.store.main_tycoon.yachts: # The store is useful for storing pointers to objects you 145 print yacht.owner.name # want access to later on -- saving you a SQL query. 146 # Using the store in this way to access the database is 147 # similar to how you access the database in other ODBMS systems 148 # like ZODB or Durus.