Piper Pickling?
Darrell
dgallion1 at yahoo.com
Fri Sep 13 08:39:21 EDT 2002
Here's one approach I've used.
Create an interface class for each class you plan to store.
Also create the schema.
--Darrell
class A:
"""
Some data class
"""
_X1=9.2
_Y1="text"
def __init__(self, *arg, **kw):
self._x=1
self._y="hello"
self._z=arg[0]
self._range=zip(range(10),range(10,20))
def show(self):
print '*'*10,self.__class__.__name__,'*'*10
for k, v in self.__class__.__dict__.items():
if not callable(v):
print "%s: %s"%(k,v)
for k, v in self.__dict__.items():
if not callable(v):
print "%s: %s"%(k,v)
class DbIforA(DbI):
def __init__(self, objClass, schema, dbName=None, tableName=None):
DbI.__init__(self, objClass, schema, dbName, tableName)
def insert(self, *v, **kw):
excludes= kw.setdefault("excludeCols",{})
excludes["_dbLocalTimeStamp"]=0
excludes['_dbLocalSerialNum']=0
apply(DbI.insert, (self,)+v,kw)
def getValues(self):
res=DbI.getValues(self)
return res
schema={
'_x':{'default':55, 'type':'integer'},
'_y':{'default':65, 'type':'varchar(30)'},
'_z':{'default':75, 'type':'integer NOT NULL',
'constraints':'Primary key '},
'_X1':{'default':None, 'type':'double'},
'_dbLocalTimeStamp':{'default':None, 'type':'TIMESTAMP(10)'},
'_dbLocalSerialNum':{'default':None, 'type':'integer not null
auto_increment', 'constraints':'key'},
'_range':{'type':'pickle'}
}
dbI=DbIforA(A, schema, 'testDb', 'testTable')
a1=A(1, x=3, y=4)
a2=A(22, x=32, y=42)
a3=A(33, x=33, y=43)
# Some simple test cases
dbI.dropDataBase(IF_EXISTS="IF EXISTS")
dbI.createDb(IF_NOT_EXISTS="IF NOT EXISTS")
dbI.use()
dbI.createTable(IF_NOT_EXISTS="IF NOT EXISTS")
dbI.insert(objs=[a1, a2, a3])
dbI.use()
dbI.select()
dbI.select(WHERE="where _X1 < 10.0 and _X1 > 9.0")
ret=dbI.getValues()
for i in ret:
i.show()
i._y='Dog'
dbI.update(objs=ret)
dbI.select()
"Robert Oschler" <Oschler at earthlink.net> wrote in message news:<dg9g9.2373$Lg2.427869 at news2.news.adelphia.net>...
> Forgive me :)
>
> If Peter Piper pickles a peck of Python packages, can he pack the pickled
> Python peckers in a proper packing place?
>
> Translated: Anybody seen a module that can override the standard pickling
> process and store and retrieve Python objects to a MySQL database?
>
> thx
>
> P.S. - Whell hell the language is named after the world's best comedy
> troupe, what did you expect, green eggs and spam? :)
More information about the Python-list
mailing list