A question on decorators

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Mar 27 05:37:01 EDT 2008


Tim Henderson a écrit :
> Hello
> 
> I am writing an application that has a mysql back end and I have this
> idea to simplify my life when accessing the database. The idea is to
> wrap the all the functions dealing with a particular row in a
> particular in a particular table inside a class. So if you have a
> table that looks like this:
> 
> id   str1       str2        pickled_data1   pickled_data2
> 0    woeif      aposf       (bin)                  (bin)
> 1    ofime      powe        (bin)                  (bin)
> ...
> n    oiew       opiwe       (bin)                  (bin)
> 
> you can access this table like this
> 
> t = Table(id) #to load a pre-entered row
> t2 = Table(id, str1, str2, data1, data2) #to create a new row
> 
> when you change a an attribute of the class like this...
> t.str1 = 'new value'
> 
> it automatically updates the database backend.

Congratulation, you just reinvented ORM. Good news is that there are 
already quite a few such packages in Python. May I recommand SQLAlchemy ?-)

> I have what I just described working. However I want an easier way to
> deal with my pickled_data. Right now I am pickling dictionaries and
> list types. Now there is one problem with this,

Indeed, but not the one you mention. The problem is that storing 
serialized dicts / lists / any other non atomic data in a blob is, well, 
not exactly the best possible use of a *relational* database.

Do yourself a favour: learn what 'relational' means, and replace your 
blobs with the appropriate tables in the database.

(snip code)

> 
> Now while this works, it is a lot of work. 

This is why it's better to use an existing package whenever possible. 
SQLAlchemy is here:
http://www.sqlalchemy.org/

(snip mode code)



More information about the Python-list mailing list