There're quite a few python ORM's. However, most are not easy to use. In Ruby on Rails's ActiveRecord ORM, you don't have to define schema, just specify the relationship like "belongs_to" and "has_many", and ORM do rest of the work, it's very easy to learn and easy to use. This recipe provide a python ORM that behave like ActiveRecord.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | # this is storm.py
import string, new, MySQLdb
from types import *
from MySQLdb.cursors import DictCursor
bag_belongs_to, bag_has_many = [],[]
def belongs_to(what): bag_belongs_to.append(what)
def has_many(what): bag_has_many.append(what)
class Mysqlwrapper:
def __init__(self,**kwds):
self.conn = MySQLdb.connect(cursorclass=DictCursor,**kwds)
self.cursor = self.conn.cursor()
self.escape = self.conn.escape_string
self.insert_id = self.conn.insert_id
self.commit = self.conn.commit
self.q = self.cursor.execute
def qone(self,query):
self.q(query)
return self.cursor.fetchone()
def qall(self,query):
self.q(query)
return self.cursor.fetchall()
class MetaRecord(type):
def __new__(cls, name, bases, dct):
global bag_belongs_to, bag_has_many
if name in globals(): return globals()[name]
else:
Record = type.__new__(cls, name, bases, dct)
for i in bag_belongs_to: Record.belongs_to(i)
for i in bag_has_many: Record.has_many(i)
bag_belongs_to = []
hag_has_many = []
return Record
class Storm(dict):
__metaclass__ = MetaRecord
__CONN = None
@classmethod
def belongs_to(cls, what):
def dah(self):
belong_cls = globals().get(what,None)
if not belong_cls:
belong_cls = type(what,(Storm,),{})
return belong_cls.selectone(self[what+'_id'])
setattr(cls,what,new.instancemethod(dah,None,cls))
@classmethod
def has_many(cls, what):
def dah(self):
hasmany_cls = globals().get(what,None)
if not hasmany_cls:
hasmany_cls = type(what,(Storm,),{})
dct={}
dct[string.lower(cls.__name__)+'_id']=self['id']
return hasmany_cls.select(**dct)
setattr(cls,what,new.instancemethod(dah,None,cls))
@classmethod
def conn(cls, **kwds):
if not cls.__CONN: cls.__CONN = Mysqlwrapper(**kwds)
@classmethod
def exe(cls,s):
if not cls.__CONN: raise "Database not connected"
return cls.__CONN.qall(s)
@classmethod
def insert(cls,**kwds):
vs = [[k,cls.__CONN.escape(str(kwds[k]))] for k in kwds]
if vs:
s = "insert into %s (%s) values ('%s')" % (
string.lower(cls.__name__), ','.join([v[0] for v in vs]),
"','".join([v[1] for v in vs]))
cls.__CONN.q(s)
cls.__CONN.commit()
return cls.__CONN.insert_id()
else: raise "nothing to insert"
@classmethod
def select(cls,*args, **kwds):
if len(args)==1 and (type(args[0])==IntType or type(args[0])==LongType):
q = "select * from %s where id='%s'"%(string.lower(cls.__name__),args[0])
where = "where id='%s'"%args[0]
else:
if args: s = ",".join(args)
else: s = "*"
if kwds:
c,limit,orderby = [],'',''
for k in kwds:
if k == 'limit': limit = "limit "+str(kwds[k])
elif k == 'order': orderby = "order by "+str(kwds[k])
else: c.append(k+"='"+str(kwds[k])+"'")
where = " and ".join(c)
if where: where = "where %s"%where
where = "%s %s %s"%(where,orderby,limit)
else: where = ""
q = " ".join(['select',s,'from',string.lower(cls.__name__),where])
r = cls.__CONN.qall(q)
list = []
for i in r:
list.append(cls(i))
list[-1].__dict__['where'] = where
return list
@classmethod
def selectone(cls,*args, **kwds):
r = cls.select(*args,**kwds)
if r: return r[0]
else: return {}
@classmethod
def update(cls,cond,**kwds):
if not cond or not kwds: raise "Update What?!"
if type(cond) == IntType: w = "id='%d'" % cond
else: w = cond
vs = [[k,cls.__CONN.escape(str(kwds[k]))] for k in kwds]
if vs:
s = "UPDATE %s SET %s WHERE %s" % ( string.lower(cls.__name__),
','.join(["%s='%s'"%(v[0],v[1]) for v in vs]), w)
cls.__CONN.q(s)
cls.__CONN.commit()
@classmethod
def delete(cls,id):
if type(id) == IntType:
cls.__CONN.q("delete from %s where id='%d'"%
(string.lower(cls.__name__),id))
cls.__CONN.commit()
else: raise "Only accept integer argument"
def __init__(self,dct={}):
if not self.__class__.__CONN: raise "Database not connected"
dict.__init__(self,dct)
self.__dict__['cur_table']= string.lower(self.__class__.__name__)
self.__dict__['where']= ''
self.__dict__['sql_buff']={}
def sql(self,sql): self.__class__.__CONN.q(sql)
def save(self):
s = ""
if self.where:
f = []
for v in self.sql_buff:
f.append("%s='%s'"%(v,self.sql_buff[v]))
s = "UPDATE %s SET %s %s" % (
self.cur_table, ','.join(f), self.where)
else:
f,i=[],[]
for v in self.sql_buff:
f.append(v)
i.append(self.sql_buff[v])
if f and i:
s = "INSERT INTO %s (%s) VALUES ('%s')" % (
self.cur_table, ','.join(f), "','".join(i))
if s:
self.__class__.__CONN.q(s)
self.__class__.__CONN.commit()
else: raise "nothing to insert"
def __setattr__(self,attr,value):
if attr in self.__dict__: self.__dict__[attr]=value
else:
v = self.__class__.__CONN.escape(str(value))
self.__dict__['sql_buff'][attr] = v
self[attr] = v
def __getattr__(self,attr):
if attr in self.__dict__: return self.__dict__[attr]
try: return self[attr]
except KeyError: pass
raise AttributeError
__all__ = ['Storm', 'belongs_to', 'has_many']
#----------------- end of storm.py ----------------
Below is a session screenshot of using this ORM(Storm):
-------------------------------------------------------------
wang@dapper-03:~/spark/lib$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 46 to server version: 5.0.22-Debian_0ubuntu6.06-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database teststorm;
Query OK, 1 row affected (0.00 sec)
mysql> use teststorm;
Database changed
mysql> create table author(id int auto_increment primary key,name varchar(50));
Query OK, 0 rows affected (0.06 sec)
mysql> create table book(id int auto_increment primary key,author_id int,title varchar(100));
Query OK, 0 rows affected (0.01 sec)
mysql> describe author;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> describe book;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| author_id | int(11) | YES | | NULL | |
| title | varchar(100) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> Bye
wang@dapper-03:~/spark/lib$ python
Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from storm import *
>>> class Author(Storm):
... has_many('book')
...
>>> class Book(Storm):
... belongs_to('author')
...
>>> Storm.conn(user='root',db='teststorm')
>>> a = Author()
>>> a.name = 'Tolstoy'
>>> a.save()
>>> Author.insert(name='Charles Dickens')
0L
>>> a.name, a['name']
('Tolstoy', 'Tolstoy')
>>> o = Author.selectone(2)
>>> o
{'id': 2L, 'name': 'Charles Dickens'}
>>> o.id, o.name, o['id'], o['name']
(2L, 'Charles Dickens', 2L, 'Charles Dickens')
>>> b = Book()
>>> b.author_id = 1
>>> b.title = 'Anna Karenina'
>>> b.save()
>>> b.title = 'War and Peace'
>>> b.save()
>>> b.author_id = 2
>>> b.title = 'Great Expectations'
>>> b.save()
>>> Book.insert(author_id=2,title='A Tale of Two Cities')
0L
>>> Book.insert(author_id=2,title='David Copperfield')
0L
>>> all = Book.select()
>>> all
[{'author_id': 1L, 'id': 1L, 'title': 'Anna Karenina'}, {'author_id': 1L, 'id': 2L, 'title': 'War and Peace'},
{'author_id': 2L, 'id': 3L, 'title': 'Great Expectations'}, {'author_id': 2L, 'id': 4L, 'title':
'A Tale of Two Cities'}, {'author_id': 2L, 'id': 5L, 'title': 'David Copperfield'}]
>>> o = Book.selectone(4)
>>> a = o.author()
>>> a
{'id': 2L, 'name': 'Charles Dickens'}
>>> a = Author.selectone(name='Tolstoy')
>>> a
{'id': 1L, 'name': 'Tolstoy'}
>>> b = a.book()
>>> b
[{'author_id': 1L, 'id': 1L, 'title': 'Anna Karenina'}, {'author_id': 1L, 'id': 2L, 'title': 'War and Peace'}]
>>> b[0].title, b[1].title
('Anna Karenina', 'War and Peace')
>>>
wang@dapper-03:~/spark/lib$
|
You create class that has the same name as db table. You can use classmethod insert() or create an instance to insert to db. You get objects of query result using select() and selectone() class methods. You can define "belongs_to" and "has_many" one-to-many relationship during class definition. They automatically add instance method that return the object of associated table. For example, when we define "class Book(Storm): belongs_to('author')", it add a instance method author() that return the object of class "Author" (whois id = instance_book.author_id). This metaprogramming technique is from one of commentor "brantley" in this discussion: http://blog.ianbicking.org/more-on-python-metaprogramming.html
The ORM also has update(), delete() class methods.
This ORM is sufficient for use in small db's that has simple relationship. For complex database, I wouldn't use ORM anyway, most likely will use dbapi directly instead.
This ORM use Mysql, it can be modified to work with Sqlite and Postgres easily.