how to pass an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diwakar09
    New Member
    • Sep 2007
    • 39

    how to pass an object

    hi,

    could you please help me in passing object to memcache set() function
    actuallly i want to pass my class object as a key to memcache set(key,value) function,
    i have tried str() function but i doesn't work
    if you can provide a sample code that will be good for me
    i am using python 2.5 on my ubuntu machine.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by diwakar09
    hi,

    could you please help me in passing object to memcache set() function
    actuallly i want to pass my class object as a key to memcache set(key,value) function,
    i have tried str() function but i doesn't work
    if you can provide a sample code that will be good for me
    i am using python 2.5 on my ubuntu machine.
    If you will provide a snippet, we'll be glad to take a look at it.

    Comment

    • diwakar09
      New Member
      • Sep 2007
      • 39

      #3
      Originally posted by bartonc
      If you will provide a snippet, we'll be glad to take a look at it.
      Code:
      import os
      import memcache
      import helper
      import time
      from dThread import dThread
      import cPickle as pickle
      CClientArray = []
      Objectke = []
      #j = 1
      def to_s(val):
          """
          Convert val to string.
          """
          if not isinstance(val, str):
              return "%s (%s)" % (val, type(val))
          return val
      class CClient(dThread):
          mc = None
          newval = None
          #ival = None
          ObjectKey = None
          def __init__(self):
              try:
                  dThread.__init__(self)
                  self.mc = memcache.Client(['192.168.1.68:11211'], debug=0)
                  self.newval = None
                 # self.ival = i
                  self.Objectkey = None
                  #self.ival = i
                 # self.logfile = file(filename, 'w')
              except:
                  print "in constructor"
      
          #def __init__(self, value, filename):
           
      
          
          def setValue(self, ivalue):
              try:
                  #temp = Objectke[ivalue]
                
                  #key = str(ivalue)
                  
                  value = "CClientclass for new test" #+ str(j)
                  # j = j+1
                  self.mc.set(ivalue, value)
                 
              except:
                  print "in setvalue"
             
          def showVal(self):
              print self.newval
      
          def getValue(self,ivalue):
              try:
                  temp = Objectke[ivalue]
                  #print temp
                  #i = str(temp)
                  #print self.mc.get(temp)
                  #showVal();
              except:
                  print "in getvalue"
                  
          def setObjectValue(self, key):
              self.Objectkey = key
              
          def getObjectValue(self ):
              return self.Objectkey 
          
      
      def startClient():
          mc = memcache.Client(['192.168.1.68:11211'], debug=0)
          mc.flush_all()
          for i in range(1, 10):
              obj = CClient()
              CClientArray.append(obj)
              try:
                 temp = pickle.dumps(obj)
                 obj.setValue(temp)
              except:
                  print "error in appending"
              
          print "after set the value  "
      """    
          for i in range(1, 10):
              obj = CClientArray[i]
              obj.getValue(i)
      
          print "after getting the value"
      """
      Thanx for replying me,
      i just want to pass object of CClient class int memcache set function as a key,

      if you can make this code work this will be good for me.
      Thanx

      Comment

      Working...