- A Simple Example
- Detecting Changes
- A Complete Example
- Conclusion
- ZODB Resources
A Complete Example
The following is a complete example program. It builds on the employee examples used so far.
Listing 1 A Complete Example Program
from ZODB import DB from ZODB.FileStorage import FileStorage from ZODB.PersistentMapping import PersistentMapping from Persistence import Persistent class Employee(Persistent): """An employee""" def __init__(self, name, manager=None): self.name=name self.manager=manager # setup the database storage=FileStorage("employees.fs") db=DB(storage) connection=db.open() root=connection.root() # get the employees mapping, creating an empty mapping if # necessary if not root.has_key("employees"): root["employees"] = {} employees=root["employees"] def listEmployees(): if len(employees.values())==0: print "There are no employees." print return for employee in employees.values(): print "Name: %s" % employee.name if employee.manager is not None: print "Manager's name: %s" % employee.manager.name print def addEmployee(name, manager_name=None): if employees.has_key(name): print "There is already an employee with this name." return if manager_name: try: manager=employees[manager_name] except KeyError: print print "No such manager" print return employees[name]=Employee(name, manager) else: employees[name]=Employee(name) root['employees'] = employees # reassign to change get_transaction().commit() print "Employee %s added." % name print if __name__=="__main__": while 1: choice=raw_input("Press 'L' to list employees, 'A' to" "add an employee, or 'Q' to quit:") choice=choice.lower() if choice=="l": listEmployees() elif choice=="a": name=raw_input("Employee name:") manager_name=raw_input("Manager name:") addEmployee(name, manager_name) elif choice=="q": break # close database connection.close()
This program demonstrates a couple of interesting things. First, this program shows how persistent objects can refer to each other. The self.manager attribute of employee instances can refer to other employee instances. Unlike a relational database, there is no need to use indirection, such as object ids, when referring from one persistent object to another. You can just use normal Python references. In fact, you can even use circular references.
A final trick used by this program is to look for a persistent object and create it if it is not present. This allows you to just run this program without having to run a setup script to build the database first. If there is no database present, the program will create one and initialize it.