0

Good day! I trying to go the method of web-service using reflection. Here is an example of code:

...

api = cf.SomeServiceAPI()

#Test1
def test_SomeMethod(self):
   result = self.sender('SomeMethod', [setofvalue])
   self.assertEqual(result, "Success", msg=result)

def sender(self, methodname, setofvalue):
   result = self.api.service.SomeMethod(setofvalue)
   return result

Please help me understand how to apply the method using method's name? Thanks!

3 Answers3

0

looks like this is a duplication of this question.

You should use:

getattr(object, 'method_name')
Community
  • 1
  • 1
AndreyF
  • 1,798
  • 1
  • 14
  • 25
0

You can use getattr(clazzA, methodname)(setofvalue) where clazzA is the object, methodname is the name of the method in string and setofvalue is the parameter you want to pass into the method.

Here is an example of your requested behavior:

class A:
  def some_method(self, arg):
    print ("in: ", arg)

#Test1
def test_Some_method():
   result = sender('some_method', "method")

def sender(methodname, setofvalue):
   clazzA = A()
   result = getattr(clazzA, methodname)(setofvalue)
   return result

test_Some_method()

>>>'in:  method'
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • Thanks! But I donn't have an explicit class A. All methods defined in the suds..px files. Their definition is done automatically using wsdl. – Aleksey Krekotnev Jan 24 '17 at 07:51
0

I solved the task.

...
api = cf.SomeServiceAPI()
m1 = api.service.__getattr__('SomeMethod')

#Test1
def test_SomeMethod(self):
   result = self.sender(self.m1, [setofvalue])
   self.assertEqual(result, "Success", msg=result)

def sender(self, methodname, setofvalue):
   result = method(setofvalue)
   return result