Good day everyone! And please tell me how in python, or in general, replace the following:

if sys.argv[1] == "restart_ca": client = JsonClient(str(sys.argv[2]), 8081) client.connect() ... elif sys.argv[1] == "stop_ca": client = JsonClient(str(sys.argv[2]), 8081) client.connect() ... elif sys.argv[1] == "start_ca": client = JsonClient(str(sys.argv[2]), 8081) client.connect() ... elif sys.argv[1] == 'set_reg_key_net_int': client = JsonClient(str(sys.argv[2]), 8081) client.connect() ... 

sys.argv [1] is the passed method through the argument

And there are a lot of such checks in the program (Is there a way to simplify such a scenario? Tell me please.

  • As a result, the same code is executed. Why check then? - user207618
  • one
    The problem with this issue is not this. This is what I will endure and so. I don't know how you can replace the numerous uses of if. I will have many different methods in the program - and each time the program is started - it checks whether these passed methods match the condition and execute the necessary code. I would like to simplify it somehow, but I still don’t understand how - Vorobey.A
  • 2
  • one
    Well, yes, or through the dictionary. - Mr. Fix
  • one
    How about this: repl.it/EWaJ/0 ? - user207618

3 answers 3

Decided as follows:

 def stop_service(): pass def restart_service(): pass def start_service(): pass def set_reg_key(): pass def disable_agent_adapter_net_int(): pass def enable_agent_adapter_net_int(): pass method = { "stop-service": stop_service, "restart-service": restart_service, "start-service": start_service, "set-reg-key": set_reg_key, "disable-agent-adapter-net-int": disable_agent_adapter_net_int, "enable-agent-adapter-net-int": enable_agent_adapter_net_int } method["stop-service"]() 

And in case you need to add a new method, you can very simply add the method key to the dictionary and the corresponding method - very simply and without any big branching))

    You can use a dictionary, in particular, if there are corresponding functions (of type restart_ca , stop_ca ) in the global namespace:

     command_name = sys.argv[1] # get the command name command = globals()[command_name] # find the command command() # run the command 

    Perhaps there is already a module that helps to create applications that take arguments from the command line (such as click ), where a similar function is already built in. For example, using the command decorator you can register:

     @command def start_ca(): ... 

    If the functions / methods with the specified names are defined for another object — a regular instance of the class or, in particular, another module:

     command = getattr(obj, command_name) 
       class Source1: def restart_ca(): return JsonClient(str(sys.argv[2]), 8081) def stop_ca(): return JsonClient(str(sys.argv[2]), 8081) def start_ca(): return JsonClient(str(sys.argv[2]), 8081) def checker(argv, source=Source1): try: return getattr(source, argv) except AttributeError: return None assert checker(sys.argv[1]) 
      • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky