/test/front_page.py

http://pickled-object-database.googlecode.com/ · Python · 148 lines · 88 code · 49 blank · 11 comment · 20 complexity · 5478eef96b98ea6f83a1dcf1172b7289 MD5 · raw file

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