Now we are making a set of automatic tools for programs on the server, with various operations such as starting, stopping, and adjusting. However, the deployment content on different nodes is not only the same, it is assumed that python functions with various functions have been prepared. Then read external information, such as a json data, to determine the running order and parameters of these functions. Can you please tell me how to achieve this?
For example, there are three business functions: Function Function 1 (Parameter 1, Parameter 2) Function Function 2 (Parameter 1, Parameter 2, Parameter 3) Function Function 3 (Parameter 1).
Then I read a {{{'funcname':'Function function 2'},{'parameter':[para1,para2,para3]}},{{'funcname':'Function function 1'}, {'parameter':[para1,para2]}}}
Then the program will follow the order of function 2, function 1, and read the parameter parameters to execute this series of actions.
(In fact, as an automation tool, it must be timely feedback information. After each function is executed, the return information will be read. If an error occurs, the program will be interrupted immediately, and the information will be reported or logged, so that is to say, every time it is executed After completing a functional function, a simple program for judging the return result will also be executed)
It should be noted here that the number of functions to be executed is uncertain, and the order is also uncertain. The stupidest way that I immediately thought of was to traverse the external json file and then read the funcname to determine which function to execute. But I feel that this method is more troublesome to expand and maintain later. Hope we can discuss it
女神的闺蜜爱上我2017-06-14 10:55:27
Use eval to execute a string as code and return the result:
def func1(a,b):
return a+b
s = eval("func1(1,2)")
print(s)
# 3
Use a loop to traverse all items of the Json file
Convert function name and parameters into strings.
eval execution string
Processing return values