Michal Wallace sabren at
Tue Feb 12 22:00:45 CET 2002

[sorry if this is sent twice - i had mailman issues...]

Hey all,

This is from an idea I'm working on at:

It concerns a scheme for somewhat strongly typed python,
including getters and setters... There's no code, but I'm
posting it here because I saw Guido's slides from the
conference, and noticed that the development team is
thinking about adding this kind of thing in a future python
version... The strongbox concept offers these things through

(It also describes a scheme that uses the Observer pattern
to persist objects to various backends (relational, ZODB,


I call it strongbox because it's strongly typed. Python
variables are weakly typed, which means x can be a string
one minute, and an integer the next. That's great most of
the time, but it's a pain for working with relational
databases, because the columns hold only one type of
data. So my zdc applications are full of validation code
that gets repeated all over the place. Data validation is
one of the biggest things slowing me down when I go to write
a web app.

Strongbox changes everything. It starts with a metaclass
called, not surprisingly, Strongbox. When you subclass
Strongbox, you don't get a normal python class, but a
Strongbox class. Strongboxes have built-in getters and
setters. They also let you define strongly-typed slots like

from strongbox import Slot 
from strongbox import Strongbox 
from pytypes import FixedPoint 
class Person(Strongbox): 
    fname  = Slot(str, size=15, default="herman") 
    salary = Slot(FixedPoint) 
    favorite = Slot(str, valid=('red','blue','green')) 
    age = Slot(int, valid=lambda x: x > 0) 
    def get_canDrink(self): 
        return self.age > 21 
>>> fred = Person(fname='fred', age=18) 
>>> fred.canDrink 
>>> fred.age = 25 
>>> fred.canDrink 

The "valid" argument lets you specify rules for valid
data. I plan to support regular expressions, enums, and
lambdas. (If you need a full function, you can just override

The "pytypes" module mentioned above is also a child of
zdc... I'm taking all the datatypes defined there (Date,
DateTime, IdxDict, FixedPoint) and putting them into their
own module.

Another neat thing about Strongbox is the Observer
pattern. Notice that in the example, fred is just a
freestanding object. No database was needed. Fred doesn't
know anything about databases, but he can be saved to
one. The simplest way is to pickle him and store him in a
blob or a file. (Not 100% sure pickling works with
metaclasses, but we'll find out). But we can also store
fred's slots in a relational database.

To do that, we create a special observer called a Clerk. As
Fred changes, he fires off events notifying anyone who cares
to listen that something has happened. The Clerk listens to
these events, and can therefore decide whether or not he
needs to be saved. This means you don't actually have to
call "save" every time something changes. You *can* do that
if saving is expensive, but you can also just have the Clerk
save fred to the database.

Which database, though? It doesn't matter. Clerk doesn't
know about databases either, it knows about Sources. Source
is just an interface, and there will be different Sources
for different types of storage systems. DBAPI2Source will
map strongboxes to a database like MySQL.

With zdc, the object-relational mapping required all sorts
of work, but with strongbox, it's simple. You've defined
your Slots in python, so there's no need to investigate the
table. Mapping classes to tables can be done with a simple
python dictionary. In fact, we can even generate the SQL
statements for the tables from the class definition.

The best part of all this is that most of it is already
done. I expect Strongbox to be a fraction of the size of
zdc, and it should only take a day or two to implement



- Michal   sabren at 
Give your ideas the perfect home:
 cvs - weblogs - php - linux shell - perl/python/cgi - java

More information about the Python-list mailing list