Operation and maintenance automation - How to use externally read file contents to determine the execution order and parameters of python functions?
PHP中文网
PHP中文网 2017-06-14 10:53:27
0
1
888

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

PHP中文网
PHP中文网

认证高级PHP讲师

reply all (1)
女神的闺蜜爱上我

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
  1. Use a loop to traverse all items of the Json file

  2. Convert function name and parameters into strings.

  3. eval execution string

  4. Processing return values

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!