You could also more simply do def youraction(): import cPickle myobj = session.myobj and cPickle.loads(session.myobj) .... use myobj .... session.myobj=cPickle.dumps(myobj)
On May 13, 8:36 pm, lesh <markl...@marklesh.com> wrote: > Just to follow up on this topic... > > In order to remedy this problem storing objects in the session, I > ended up writing an InlinePickler module: > > #Copyright (c) 2009 by Mark D. Lesh (www.marklesh.com) > #All rights reserved. > # > #InlinePickler is Licensed under GPL version 2.0 > #<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> > > import os > import pickle > from StringIO import StringIO > > class InlineUnpickler(pickle.Unpickler): > """ > Usage: > > #To setup InlinePickler for TestClass: > #import the InlinePickler.py module > from applications.ApplicationName.modules import > InlinePickler > > #Define your class with the following serialization > methods > class TestClass: > def serialize(self): > return InlinePickler.dumps(self) > > def deserialize(self,serialized_class): > return InlinePickler.loads > (serialized_class,self) > > def __init__(self): > pass > > #To store TestClass instance to session: > #create your object's name in the session > session.test_obj={} > > #create your instance and populate it > x = TestClass() > x.list=['1','2','3'] > > #store it by calling the instance's serialize method > session.test_obj=x.serialize() > > #To retrieve the TestClass instance from the session > #Instantiate the class, and call the deserialize > method > x= TestClass().deserialize(session.test_obj) > """ > > def set_custom_class_type(self,custom_class_type): > self.custom_class_type=custom_class_type > > def load_global(self): > self.append(self.custom_class_type) > > def find_class(self, module, name): > if (self.custom_class_type!=None): > return self.custom_class_type > else: > __import__(module) > mod = sys.modules[module] > klass = getattr(mod, name) > return klass > > def loads(str,instance): > file = StringIO(str) > inline_pickle=InlineUnpickler(file) > inline_pickle.set_custom_class_type(instance.__class__) > return inline_pickle.load() > > def dumps(instance): > return pickle.dumps(instance) > > On May 13, 1:24 am, mdipierro <mdipie...@cs.depaul.edu> wrote: > > > You cannot store your own classes in thesession. This is because > > thesessionis retrieved automatically for you before your controller is > > called and outside the scope where the function is defined. > > > This is a general problem with Python. > > > Try this: > > import picke > > class A: pass > > pickle.dump(A(),open('afile.pickle','w')) > > > Then from a DIFFERENT program > > import pickle > > print pickle.load(open('afile.pickle','r)) > > > the load will fail because class A is not define there. > > > Massimo > > > On May 13, 12:14 am,lesh<markl...@marklesh.com> wrote: > > > > I am having difficulty storing a simple (non-SQL) object in the > > >session. I wondered if anyone could give me a clue as to why. The > > > following works: > > > > def update_profile(): > > > if (session.sections!=None): > > > session.sections=None > > > return dict(sections='spam') > > > else: > > > session.sections={} > > > #x = testClass() > > > x = 'eggs' > > > session.sections=x > > > return dict(sections=`x`) > > > > class testClass: > > > pass > > > > with a toggling result on successive reloads of: > > > > ' e g g s ' > > > <<RELOAD THE PAGE>> > > > s p a m > > > ... > > > > However if I swap the two lines as below: > > > > def update_profile(): > > > if (session.sections!=None): > > > session.sections=None > > > return dict(sections='spam') > > > else: > > > session.sections={} > > > x = testClass() > > > #x = 'eggs' > > > session.sections=x > > > return dict(sections=`x`) > > > > class testClass: > > > pass > > > > The result fails to toggle: > > > > <__b u i l t i n__.testClassinstanceat0xab18ccc> > > > <<RELOAD THE PAGE>> > > > <__b u i l t i n__.testClassinstanceat0xab094cc> > > > ... > > > > Which appears that the object doesn't ever actually store into the > > >session. I searched the group and from what I can tell this is > > > possible, but I can't figure out what I'm doing wrong. > > > > Thanks in advance! > > > > --Lesh --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py Web Framework" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---