Object Database (ODBMS) for Python
Patrick K. O'Brien
pobrien at orbtech.com
Thu Aug 28 20:38:50 EDT 2003
"Pettersen, Bjorn S" <BjornPettersen at fairisaac.com> writes:
> > From: Patrick K. O'Brien [mailto:pobrien at orbtech.com]
> >
> > I'm working on an ODBMS written in Python, for Python, and was
> > wondering if anyone was interested. In particular, I'd like to know
> > what features would be useful, and what types of use cases people
> > would have for a simple, but feature-rich object database.
> >
> > The system that I'm developing is PyPerSyst, which began as a simple
> > persistence mechanism, but is now becoming a complete ODBMS. Some
> > details are available here:
> >
> > http://www.orbtech.com/wiki/PyPerSyst
> >
> > The code is available in CVS on SF:
> >
> > http://sourceforge.net/projects/pypersyst/
>
> I'd be interested, but can't seem to find docs, demos or tests through
> sf's web interface.. any pointers?
First of all, let me just make a caveat that this is still in the
early stages of development. By that I mean that many features are
coded, and there are a good many unit tests, but I haven't got much in
the way of docs and demos. PyPerSyst is being used in a commercial
application, so it does work quite well. But in no way am I
advertising it as a finished product. I'm just looking for feedback
from early adopters and developers with an interest.
The main pypersyst package is here:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/pypersyst/pypersyst/pypersyst/
The unit tests are here:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/pypersyst/pypersyst/pypersyst/test/
I'm working on a simple demo (twistedcred), but haven't committed it
to cvs yet. In the mean time, here is what the database portion of an
application looks like:
import os
from pypersyst.database import Database
from pypersyst.engine.engine import Engine
from pypersyst.storage.storage import Storage
from twistedcred import data
from twistedcred.schema import cred
def database():
"""Return a PyPerSyst database."""
dir = os.path.dirname(data.__file__)
app = '.twistedcred'
storage = Storage(dir, app, binary=False, python=True)
engine = Engine(storage, cred.Root)
database = Database(engine)
return database
---
And here is the schema for the twistedcred database:
from pypersyst import root
from pypersyst.entity.entity import Entity
class Avatar(Entity):
"""Avatar class."""
_attrSpec = [
'realm',
'user',
'name',
]
_altkeySpec = [
('user', 'realm', 'name',),
]
def __init__(self, user, realm, name='Avatar'):
"""Create Avatar instance."""
self._prep(locals())
Entity.__init__(self)
class Realm(Entity):
"""Realm class."""
_attrSpec = [
'name',
]
_altkeySpec = [
('name',),
]
def __init__(self, name):
"""Create Realm instance."""
self._prep(locals())
Entity.__init__(self)
class User(Entity):
"""User class."""
_attrSpec = [
'name',
'hashedPassword',
]
_altkeySpec = [
('name',),
]
def __init__(self, name, hashedPassword=None):
"""Create User instance."""
self._prep(locals())
Entity.__init__(self)
class Root(root.Root):
"""Root class."""
_EntityClasses = [
Avatar,
Realm,
User,
]
You can create the database using PyCrust, for example, and interact
with it like this:
>>> from twistedcred.database import database
>>> db = database.database()
>>> from pypersyst.entity import transaction as tx
>>> t = tx.Create('User', name='Bob')
>>> u1 = db.execute(t)
>>> u1.name
'Bob'
>>> t = tx.Create('Realm', name='Whatever')
>>> r1 = db.execute(t)
>>> t = tx.Create('Avatar', name='MyAvatar', user=u1, realm=r1)
>>> a1 = db.execute(t)
>>> a1.user.name
'Bob'
>>> t = tx.Create('User', name='Bob')
>>> u = db.execute(t)
Traceback (most recent call last):
File "<input>", line 1, in ?
File "/home/pobrien/Code/pypersyst/database.py", line 27, in execute
return self._engine.execute(transaction)
File "/home/pobrien/Code/pypersyst/engine/engine.py", line 75, in execute
return transaction.execute(self._root)
File "/home/pobrien/Code/pypersyst/entity/transaction.py", line 31, in execute
return self.EntityClass(**self.attrs)
File "/home/pobrien/Code/twistedcred/schema/cred.py", line 65, in __init__
Entity.__init__(self)
File "/home/pobrien/Code/pypersyst/entity/entity.py", line 81, in __init__
self.extent._insert(self)
File "/home/pobrien/Code/pypersyst/entity/extent.py", line 213, in _insert
self._validate(instance, instance._attrs())
File "/home/pobrien/Code/pypersyst/entity/extent.py", line 325, in _validate
self._validatekeys(instance, attrs)
File "/home/pobrien/Code/pypersyst/entity/extent.py", line 335, in _validatekeys
raise KeyError, msg
KeyError: duplicate value ('Bob',) for altkey ('name',)
>>> u1.links
{('Avatar', 'user'): [<twistedcred.schema.cred.Avatar object at 0x88a6294>]}
>>> r1.links
{('Avatar', 'realm'): [<twistedcred.schema.cred.Avatar object at 0x88a6294>]}
>>> db.root['Avatar'].match(name='Ava')
[]
>>> db.root['Avatar'].search(name='Ava')
[<twistedcred.schema.cred.Avatar object at 0x88a6294>]
>>>
I hope that helps demonstrate some of what it can do.
--
Patrick K. O'Brien
Orbtech http://www.orbtech.com/web/pobrien
-----------------------------------------------
"Your source for Python programming expertise."
-----------------------------------------------
More information about the Python-list
mailing list