OOPS - Python Datastore
Object Oriented Database Library For Persistent Storage in Python
Sections
Using OOPS:

The Obj class and units:
OOPS' power comes from extending its classes to suit your particular needs. You extend OOPS classes by ways of inheritance. The most important class you should remember is the Obj class. Those of you from an SQL background can view an Obj subclass as a table definition and an instance of that class as a row from that table. Here's an example of an address object:

>>> class PostalLocation(Obj):
... 	street = Text(100)
... 	city = Text(25)
... 	region = Text(40)
... 	country = Text(40)
...
Text is a subclass of Unit which is the base class to all persistent attributes (aka units). Text stores values of the UnicodeType (and translates StringType to UnicodeType automatically). Text takes an optional integer as a first argument. That argument specifies the maximum size of strings that can be stored within.

The behavior of an Obj subclass:
Now that address object will behave just as you expect it to:

>>> addr = PostalLocation()
>>> # assigning values
>>> addr.street = "123 Main Str."
>>> addr.city = "Montr&eacut;al"
>>> addr.region = "Qu&eacut;bec"
>>> addr.country = "Canada"
>>> # retrieving & printing values
>>> def PrintAddress(addr):
... 	"Prints a PostalLocation object to screen"
... 	print addr.street
... 	print addr.city
... 	print addr.region
... 	print addr.country
...
>>> PrintAddress(addr)
123 Main Str.
Montr&eacut;al
Qu&eacut;bec
Canada

Further exploiting units:
Units have other features than maximum size. Most are available by using keyword arguments. One the most important keyword that most units use is the constraint argument. constraint must be a callable object that returns true (1) or false (0) depending on whether or not the value is valid for that field. Here's a more detailed address object with regular expression checking that validates a Canadian postal code:

>>> class CanadianLocation(Obj):
... 	street = Text(100)
... 	city = Text(25)
... 	province = Text(22)
... 	postal_code = Data(constraint=
... 	lambda value: re.match(r"[a-z][0-9][a-z] [0-9][a-z][0-9]"))
...

Extending your own classes:
You can also inherit from your own types:

>>> class Client(PostalLocation):
... 	name = Text()
... 	phonenum = Data()

Here's a big example with methods on your objects.

>>> class Patient(Obj):
...     "Medical patient."
...     first_name = Text()
...     last_name = Text()
...     birthdate = Date()
...     def __init__(self, fname="", lname="", bdate=None, **kwargs):
...         "Constructor"
...         Obj.__init__(**kwargs) # OOPS uses keyword arguments
...         self.comments = List()
...         if fname: self.first_name = fname
...         if lname: self.last_name = lname
...         if bdate: self.birthdate = bdate
...     def display(self):
...         "print medical patient information"
...         print "Name: %s, %s" % (self.last_name, self.first_name)
...         print "Born: %r" % self.birthdate
...         print "Comments:"
...         if len(self.comments):
...             print "\n".join(self.comments)
...         else:
...             print "None"
...
>>> patient_db = List(filename='patients.odb')
>>> patient_db.append(Patient("Dan", "Parisien", (5,5,1980)))
>>> del patient_db # close file
>>> reloaded_db = List(filename='patients.odb')
>>> reloaded_db[0].display() # displays patient we just added
Name: Parisien, Dan
Born: 05/05/1980
Comments:
None
>>> reloaded_db[0].comments.append("Keeps saying oops") # changed in place
>>> reloaded_db[0].display()
Name: Parisien, Dan
Born: 05/05/1980
Comments:
Keeps saying oops
>>> db_copy = List(filename='patients-copy.odb', copyfrom=reloaded_db)
>>> if db_copy==reloaded_db:
...     print "They are equal"
... else:
...     print "They are not equal"
* Add More Stuff! *
Intro
At Sourceforge
Tutorial
API Docs
Download
Alternatives
Thanks:
eEvolved Real-Time Tech
SourceForge Logo
Python
All logos and trademarks in this site are property of their respective owner(s).
Everything else, © 2002 Dan Parisien