Simple Object/Relational mapping in Python

Introduction

When I was looking for an object-relational framework I found that there was a lot to choose from: DejaVu,SQLalchemy, SQLobject to name just the more popular ones. These are all fine and versatile but none is really dead simple.

I needed not only a simple object relational framework but also a way to create database centric applications quickly. Therefor I wrote both a simple object relational framework called DBobject, that works on top of a MySQL database and presentation framework, DBcherry, that runs as an application inside CherryPy,

Together these frameworks enable one to draft a medium sized database centric applicalion in a very short time. But let me show an example first.

Example: Animal Inventory

Say you have more than one pet, like a small herd of goats and you like to keep relevant information for each animal in a database. together with some notes a perhaps some photos. This shows how this may be coded (the code is annotated in more detail later):

from DBobject import DBobject
from DBcherry import DBcherry,serverstart

DBobject.dbdata=dict(
	db='animals',host='127.0.0.1',
	user='keeper',password='secret')

class Animal(DBobject,DBcherry):
    cols=dict(name='text',number='int')

class Note(DBobject,DBcherry):
    cols=dict(text='text')
    
serverstart(Animal)

Of course some prerequisites must be met (like having MySQL and CherryPy running) but from a coding point of view that's all there is! I have collected some screenshots to show the functionality of this app.

Prerequisites

Currently DBobject works only with MySQL. Before you can use DBobject you must make sure that you have:

DBcherry needs DBobject plus a correctly installed CherryPy package.

DBobject

Concept

The main idea is that a class representing an object and its underlying table are created in one go by inheriting from DBobject. Relations between classes may subsequently be defined. Actual tables in the database are created the moment the first object is instanced (if it doesn't already exists).

Usage examples

First import the module and set the neccessary database parameters:

from dbobject import DBobject

DBobject.dbdata=dict(db='animals',host='127.0.0.1',
			user='keeper',password='secret')
Next, define a class with properties that match columns in a table. The actual table will have the same name as the class:
class Animal(DBobject):
    cols=dict(name='text',number='int')
Finally, define other classes with their possible relations:
class Note(DBobject):
    cols=dict(text='text')
    
Animal.join(Note)
Now it is possible to create instances and work with them:
j=Animal(name='Jenny',number=200137)
j.addNote(Note(text='our best milkgoat'))

DBcherry

Concept

DBcherry should make interacting with DBobject instances just as simple as defining DBobject classes. Ideally no extra work should be done to allow the user to view, edit, search and add instances from a web application. Of course, when there are special requirements (like login authentication our specific ways to display an item) it is possible to add these or to override class methods.

Usage examples

Given our previous definition of an 'Animal' and a 'Note' class, converting these to a webapp is done like this:

from DBobject import DBobject
from DBcherry import DBcherry,serverstart

DBobject.dbdata=dict(
	db='animals',host='127.0.0.1',
	user='keeper',password='secret')

class Animal(DBobject,DBcherry):
    cols=dict(name='text',number='int')

class Note(DBobject,DBcherry):
    cols=dict(text='text')
    
Animal.join(Note)

serverstart(Animal)
This will start a webapplication on localhost:8080. The neccessary code changes are highliqhted.

More information

Of course there is more possible: check the docstrings in the sources or browse the information online. An overview of the possibilities in plain English is also available.

If you have questions or comments, please feel free to contact me at mail address (a bit smudged to prevent automatic harvesting of mailaddresses).

Extenal links

Sections

Recommended