Wallace wraps database adapters for easy connection handling and data modeling in Python apps. We extend the enterprise libraries but do not override or replace their functionality, so performance profiles etc. remain intact. Major features include:
- Databases: Currently supports PostgreSQL (psycopg), Redis (redispy), and MongoDB (pymongo). More to come
- Modeling: A bare-bones ORM, built around a consistent type interface to model attributes across backends. Use of the ORM is optional, other database and config utilities can be used without it.
- Caching: Automatic connection pool sharing - set it and forget it
To spin up a Postgres connection pool, pass DNS connection info and an optional min/max number of connections:
>>> from wallace import PostgresPool
>>> dns = {'host': '/tmp/', 'database': 'postgres', 'user': 'postgres', 'password': ''}
>>> pool = PostgresPool.construct(**dns) # defaults to max 1 connection in the pool
>>>
>>> # or, specifying a max pool size:
>>> pool = PostgresPool.construct(maxconn=5, **dns)
>>>
>>> # name the connection if you would like to cache it
>>> pool = PostgresPool.construct(name='my_db', **dns)
To use the standard interface, wrap a table:
>>> from wallace import PostgresTable
>>> class UserTable(PostgresTable):
>>> db_name = 'my_db' # specified in `PostgresPool.construct` above
>>> table_name = 'user'
>>>
>>> UserTable.add(name='guido', email='[email protected]')
>>> UserTable.fetchall()
[{'name': 'guido', 'email': '[email protected]'}]
And create a model to plug the table like so:
>>> from wallace import PostgresModel, String
>>> class User(PostgresModel):
>>> table = UserTable
>>> name = String()
>>> email = String(pk=True) # primary key field
>>>
>>> # models may be used to retrieve existing records,
>>> u = User.fetch(email='[email protected]')
>>> u.name
'guido'
>>>
>>> # create new ones,
>>> newguy = User.construct(name='spacemanspiff', email='[email protected]')
>>> newguy.push()
>>>
>>> # and execute searches
>>> [u.email for u in User.find_all(name='spacemanspiff')]
['[email protected]']
'push' to update a model:
>>> u.email = '[email protected]'
>>> u.push()
>>>
>>> User.fetch(email='[email protected]')
Traceback (most recent call last):
...
wallace.db.base.errors.DoesNotExist
>>>
>>> print User.fetch(email='[email protected]').name
'guido'
'delete' to delete:
>>> me.delete()
>>> User.fetch(email='[email protected]')
Traceback (most recent call last):
...
wallace.db.base.errors.DoesNotExist
pip install wallace
to install the latest stable release.
Code, tutorials, and documentation for wallace are all open source under the BSD license.
Enjoy your data.