I am trying to develop remote procedure call framework in python and I have a problem with exec() function. Simplified scenario:
class Example:
def __init__():
pass
def add(self, string):
return anothermodule.func(self,string)
class Framework:
def function_call():
exec("a = exampleObject.add(string)")
It seems impossible to call a function with exec() which has another function inside of it. I have tried but I always get "wrong number of parameters". In debugging mode I don't even get transfered to the "add" function in order to see what's happening.
This however, works normally.
class Example:
def __init__():
pass
def add(self, x):
return x + 3
class Framework:
def function_call():
exec("a = exampleObject.add(8)")
def main():
framework = Framework()
framework.function_call()
exampleObject is created dynamically in another class based on parameter provided in another method. It's just the instance of the Example class. This is only simplified version of what I am trying to achieve. It does not represent my exact code. What I am trying to say is, that my exec() only works on simple operation and is somehow not able to call another method inside the original one.
EDIT
class Calculator():
def add(self,a,b):
return a + b
def testFunction(self,a,b):
return self.add(self,a,b)
def main():
# Create new client object, specifying redis server and channel
remote_calculator = rpcpyredis.Proxy("localhost", "Calculator", port=6379)
# Calling any method with client object
result = remote_calculator.testFunction(3,4)
if __name__== '__main__':
main()
class Server:
def execute_code(self, calling_function, class_type):
object = class_type()
final_function_call = 'self.result = object.' + calling_function
exec(final_function_call)
return self.result
def make_function(self, request):
args = request["params"]
arguments = ",".join((str(i) for i in args))
method_name = (request["function"])
return '%s(%s)' % (method_name, arguments)
def start():
class_type = registry.lookup(object_to_lookup_for)
calling_function = self.make_function(extracted_request)
rpc_response = self.execute_code(calling_function, class_type)