Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ladon.ladonizer import ladonize
- from ladon.compat import PORTABLE_STRING,PORTABLE_BYTES
- from ladon.types.ladontype import LadonType
- from ladon.types.typeconverter import TypeConverter
- import pickle
- class ProfileProps(LadonType):
- propname = PORTABLE_STRING
- propval = PORTABLE_STRING
- class UserProfileInfo(LadonType):
- userprops = [ ProfileProps ]
- username = PORTABLE_STRING
- age = int
- class SomeService(object):
- def getMemcacheConn(self):
- # code to fetch memcache connection object
- ...
- @ladonize(PORTABLE_BYTES, UserProfileInfo, rtype=int)
- def setProfiles(self,session_id,profile_info,**exports):
- # Get memcache connection
- mc = self.getMemcacheConn()
- # Retrieve service TypeConverter
- tc = exports['LADON_METHOD_TC']
- # Force TypeConverter to convert non-strings to (like int) to strings
- mytc = TypeConverter(tc.encoding,only_strings_to_unicode = False)
- prime_dict = profile_info.__dict__(tc=mytc)
- retval = mc.set('somekey',unicode(pickle.dumps(prime_dict),'iso-8859-1'),False)
- @ladonize(PORTABLE_BYTES,rtype=UserProfileInfo)
- def getProfiles(self,session_id,**exports):
- """Get list of current profiles for session_id
- """
- # Get memcache connection
- mc = self.getMemcacheConn()
- # Retrieve service TypeConverter
- tc = exports['LADON_METHOD_TC']
- # Force TypeConverter to convert non-strings to (like int) to strings
- mytc = TypeConverter(tc.encoding,only_strings_to_unicode = False)
- # Read raw bytes from
- profile_info = mc.get('somekey')
- prime_dict = pickle.loads(profile_info.encode('iso-8859-1'))
- pinf = UserProfileInfo(prime_dict=prime_dict, tc=mytc)
- return pinf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement