pydirs - a simple Python object database

Guido Wesdorp
Pragmagik
http://pragmagik.com/
guido@pragmagik.com

The first ideas

  • 'svnodb' - SVN object database
  • SVN turned out to be a nuisance
  • slow
  • can not remove a dir, then create a new one with the same name, without committing
  • file-system only

Pydirs

  • simple code base (< 500 lines)
  • simple, explorable storage format
  • great for debugging
  • cat, echo, grep, cp
  • uses the Py lib for file-system access

Features

  • stores pydiritems as directories
  • stores basic datatypes as plain text (repr)
  • stores anything else as pickles
  • loads fast, usable for CGI and WSGI
  • transaction support
  • change and delete hooks for logging, caching, etc.

API

  • typeregistry
    • .register(pydiritem subclass)
  • database object
    • .initialize(pydiritem instance)
    • .session() -> session instance
  • session object
    • .get(path as string) -> pydiritem instance or anything else
    • .commit() -> (changed, removed)
    • .rollback()
  • pydiritem object
    • .__getattr__(name)
    • .__setattr__(name, value)
    • .__delattr__(name)
    • some magic attributes (__db__, __relpath__, __path__, etc.)

Example

>>> import pydirs
>>> class someitem(pydirs.pydiritem):
...     pass
>>> pydirs.typeregistry.register(someitem)

>>> db = pydirs.pydir('/tmp/testdb')
>>> db.initialize(someitem())
True

>>> sess = db.session()
>>> root = sess.get('/')
>>> root.foo = 1
>>> root.bar = someitem()
>>> root.baz = [1, 2]
>>> sess.commit()
(['/tmp/testdb/baz', '/tmp/testdb/foo', '/tmp/testdb/bar'], [])

>>> sess2 = db.session()
>>> sess2.get('/').foo
1
>>> sess2.get('/foo')
1
>>> sess2.get('/bar')
<__main__.someitem object at ...>
>>> sess2.get('/baz')
[1, 2]

Release