Advertisement
Guest User

Untitled

a guest
Jul 18th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. from ladon.ladonizer import ladonize
  2. from ladon.compat import PORTABLE_STRING,PORTABLE_BYTES
  3. from ladon.types.ladontype import LadonType
  4. from ladon.types.typeconverter import TypeConverter
  5. import pickle
  6.  
  7. class ProfileProps(LadonType):
  8.     propname = PORTABLE_STRING
  9.     propval = PORTABLE_STRING
  10.  
  11. class UserProfileInfo(LadonType):
  12.     userprops = [ ProfileProps ]
  13.     username = PORTABLE_STRING
  14.     age = int
  15.  
  16. class SomeService(object):
  17.  
  18.     def getMemcacheConn(self):
  19.         # code to fetch memcache connection object
  20.         ...
  21.  
  22.     @ladonize(PORTABLE_BYTES, UserProfileInfo, rtype=int)
  23.     def setProfiles(self,session_id,profile_info,**exports):
  24.  
  25.         # Get memcache connection
  26.         mc = self.getMemcacheConn()
  27.  
  28.         # Retrieve service TypeConverter
  29.         tc = exports['LADON_METHOD_TC']
  30.  
  31.         # Force TypeConverter to convert non-strings to (like int) to strings
  32.         mytc = TypeConverter(tc.encoding,only_strings_to_unicode = False)
  33.        
  34.         prime_dict = profile_info.__dict__(tc=mytc)
  35.    
  36.         retval = mc.set('somekey',unicode(pickle.dumps(prime_dict),'iso-8859-1'),False)
  37.  
  38.  
  39.     @ladonize(PORTABLE_BYTES,rtype=UserProfileInfo)
  40.     def getProfiles(self,session_id,**exports):
  41.         """Get list of current profiles for session_id
  42.         """
  43.        
  44.         # Get memcache connection
  45.         mc = self.getMemcacheConn()
  46.  
  47.         # Retrieve service TypeConverter
  48.         tc = exports['LADON_METHOD_TC']
  49.  
  50.         # Force TypeConverter to convert non-strings to (like int) to strings
  51.         mytc = TypeConverter(tc.encoding,only_strings_to_unicode = False)
  52.        
  53.         # Read raw bytes from
  54.         profile_info = mc.get('somekey')
  55.         prime_dict = pickle.loads(profile_info.encode('iso-8859-1'))
  56.        
  57.         pinf = UserProfileInfo(prime_dict=prime_dict, tc=mytc)
  58.         return pinf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement