Filtering & Getting data out from a object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Heshan Suri
    New Member
    • Apr 2008
    • 6

    Filtering & Getting data out from a object

    Hi,
    I have written a the following python code to analyse a function or a method. I am having a script which is having a python function (add) and a class (MyClass) with a method (multiply).When I analyse it using the following method(analyze_ func) , I get an output like below.
    The problem I am having is, I need to get the value 'MyClass.multip ly' from the obj. When I do obj.__name__ , I am only getting "multiply" only.So can anyone propose me how to get the value 'MyClass.multip ly'.



    -----------------------------------------------
    # OUTPUT
    -----------------------------------------------
    object passed to func() is <unbound method MyClass.multipl y>
    Method: multiply

    ===testing code===
    result is multiply
    ===testing code===

    Method Arguments: var1 var2

    object passed to func() is <function add at 0xb65aa7d4>
    Function: add

    ===testing code===
    result is add
    ===testing code===

    Method Arguments: var1 var2

    ------------------------------------------------
    # PYTHON SCRIPT
    -----------------------------------------------
    Code:
    def analyze_func(obj, method=False):   
       str1 = ''
       global result   
       print '%s' % obj.__name__
       print 'object passed to func() is ' , obj
        
       if method:
           print 'Method: %s' % obj.__name__
           strme = '%s' % obj.__name__
           str1=strme
               
       else:
           print 'Function: %s' % obj.__name__
           strme = '%s' % obj.__name__
           str1=strme      
           
      
       arginfo = inspect.getargspec(obj)
       args = arginfo[0]
       argsvar = arginfo[1]
    
       print ''
       print '===testing code==='
       
       a = arginfo[0]    
           
       print 'result is ' , str1
       result = result + str1
    
       result = result + '@'
       
       print '===testing code==='
       print ''
    
       if args:
           if args[0] == 'self':
               print '\t%s is an instance method' % obj.__name__
               args.pop(0)
           print '\tMethod Arguments: %s' % ' '.join(args)
           if arginfo[3]:
               dl = len(arginfo[3])
               al = len(args)
               defargs = args[al-dl:al]
               print 'Default arguments:',zip(defargs, arginfo[3])
    
       if arginfo[1]:
           print '\t Positional Args Param: %s' % arginfo[1]
       if arginfo[2]:
           print '\t Keyword Args Param: %s' % arginfo[2]
Working...