PyODBM
Introduction
PyODBM is my very first contact with the python programming/scripting language. It is a package that tries to map python objects into a SQL-database. It currently supports only a mysql database.
It has only support the following very basic python types:
- types.IntType
- types.LongType
- types.FloatType
- types.StringType
Installation
The installation procedure is pretty straightforward. If you have installed python packages before, this one is certainly going to succeed i guess.
tar zxvf PyODBM-0.1b.tar.gz
cd PyODBM-0.1b
python setup.py install
Wasn't that really simple?
Usage
Well, this stuff is directly from the README.txt file in the package
Well, i hope its as easy as the installation procedure
i'll try to explain the usage with a little sample program.
#!/usr/bin/env python
import PyODBM
import PyODBM.Backends
import types
suck in our packages we are going to use
Now we have to define a class we want in the database.
class Age(PyODBM.ODBMObject):
schema={ 'name': types.StringType , 'age': types.IntType }
This is basicly it. Of course you can add lots of methods and other fancy things
But the data you want in the database should be defined in the schema dictionary.
Now the actual program. First we setup a database backend to we want to use and a ODBMProxy
proxy=PyODBM.ODBMProxy(PyODBM.Backends.MySQL(db="mydatabasename",host="databasehost",user="alibabba",passwd="verysecret"))
With this proxy object we can request, create, delete objects in the database
mynewAgeObject=proxy.New(Age,"daddy");
Notice that "daddy" is the object name
now we can fill in the name and age of this object (As Specified in the schema)
mynewAgeObject["name"]="Mr. Dead Crow"
mynewAgeObject["age"]=49
if you try do to something like this:
mynewAgeObject["age"]="a string object"
you'll get a KeyError exception
or if you try to insert a key which isnt defined in the schema like this:
mynewAgeObject["dayofbirth"]="10-02-1954"
you'll get a ValueError Exception
In some other kind of program, you migth want to fetch the same object:
anExistingAgeObject=proxy.Fetch(Age,"daddy")
if you give an objectname that doesnt exists you'll get
a NoSuchObject exception.
When everything went ok, you can play the same tricks as with the myNewAgeObject
To delete the object from the database:
proxy.Delete(anExistingAgeObject)