[ZODB-Dev] Re: Problem with documentation and distribution files.

Estaban estaban@fastmail.fm
Fri, 5 Apr 2002 04:35:43 -0600


Here is an example of a script that WORKS GREAT FIRST SHOT
using STANDALONE ZODB with NO ZOPE.

This should be included in the standalone distribution as it is a working 
example with just the Standalone 1.0 code.

Thanks!
Estaban


http://www.zope.org/Documentation/Articles/ZODB1


A Complete Example

Here's a complete example program. It builds on the employee examples used so 
far:

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(







On Friday 05 April 2002 04:13 am, Estaban wrote:
> Hey,
>
> Can you help me?
>
> The Zope standalone system seems to be working on my system now,
> but the only example file clearly uses BTree which is NOT part of the
> standard python distribution OR the standalone ZODB distribution:
>
> # Ensure that a 'userdb' key is present
> if not dbroot.has_key('userdb'):
>     import BTree
>     dbroot['userdb'] = BTree.BTree()
>
> userdb = dbroot['userdb']
>
> Can you help me out here? There are other BTREES in the standard python
> distribution, but no BTree. with a BTree() thats for sure.
>
> If you can help me, this would be a good thing to update in the docs.
>
> Thanks!
> Estaban

-- 
"People do not want the truth,
they want P.T. Barnum"
- Estaban Hill