reconstituting a class full of data from MySQL BLOB object in python -
i'm using cherrypy , seems not behave nicely when comes retrieving data stored files on server. (i asked on , nobody replied, i'm on plan b, or c...) have stored class containing bunch of data structures (3 dictionaries , 2 lists of lists related) in mysql table, , amazingly, easier thought insert binary object (longblob). turned pickle file , inserted it.
however, can't figure out how reconstitute pickle , rebuild class full of data now. database returns giant string looks pickle, how make string file-like object pickle.load(data) work?
alternative solutions: how save class blob in database, or ideas on why can save pickle of class when go load later, class seems lost. in ssh / locally, works - when calling pickle.load(xxx) cherrypy errors.
i'm plan d - if there's better way store collection of structured data fast retrieval without pickles or mysql blobs...
you can create file-like in-memory object (c)stringio:
>>> cstringio import stringio >>> fobj = stringio('file\ncontent') >>> line in fobj: ... print line ... file content but pickle usage can directly load , dump string (have @ s in function names):
>>> import pickle >>> obj = 1 >>> serialized = pickle.dumps(obj) >>> serialized 'i1\n.' >>> pickle.loads(serialized) 1 but structured data stored in database, suggest in general either use
- a table, preferable orm sqlalchemy directly mapped class or
- a dictionary, (de)serialized json
and not using pickle @ all.
Comments
Post a Comment