-1

I am trying to write multiple functions to a file based on user input. Here the problem is the code which write to a file, is not left aligned. maybe missing few basic elements.

Here is the code:

import requests

G=input("Define Count")
for k in range(0,G):
perf=G

if k==0 :
        string = """
            ### RUN ####
            def run():
                d = collections.OrderedDict()
                d['run']= 123,
                return d
            URL = "https://..../run"
            headers  = {"Content-Type":"application/json",
                        "Authorization": Token}
            payload = json.dumps([run() for n in range(%s)])
            resp = requests.post(URL, headers = headers ,data = payload))
            if resp.status_code = 200:
                print('Fail: ' + str(resp.status_code)+ str(resp.text))
            else:
                print('Pass' + str(resp.status_code)+ str(resp.text))
                """
            string = Template % (Perf)
            with open(path, 'a') as f:
                f.write(string)
elif k==1:
         string = """
            ### STOP ####
            def stop():
                d = collections.OrderedDict()
                d['STOP']= 123,
                d['wait']=20
                return d
            URL = "https://..../stop"
            headers  = {"Content-Type":"application/json",
                        "Authorization": Token}
            payload = json.dumps([stop() for n in range(%s)])
            resp = requests.post(URL, headers = headers ,data = payload))
            if resp.status_code = 200:
                print('Fail: ' + str(resp.status_code)+ str(resp.text))
            else:
                print('Pass' + str(resp.status_code)+ str(resp.text))
                """
            string = Template % (Perf)
            with open(path, 'a') as f:
                f.write(string)
  elfif k=.....

when execute in loop, I expect the output file after write, with left aligned as :

   def run():
            ....
            ....
   def stop():
           ....
           ....
   def wait():
           ....

but when i excute in loop, i do get as :

   def run():
            ....
            ....
           def stop():
                      ....
                      ....
       def run():
              ....
              ....
              def stop():
                       ....
                       ....

Am not sure why i get with irregular indent. pls help me

  • 1
    Are you sure creating functions is the best way to go about this? You can't just have the functions accept another argument to alter their behavior or something? – Carcigenicate Sep 27 '20 at 15:25
  • Does this answer your question? [Proper indentation for Python multiline strings](https://stackoverflow.com/questions/2504411/proper-indentation-for-python-multiline-strings) – Tomerikoo Sep 27 '20 at 15:26
  • You may be able to use the [`textwrap.dedent()`](https://docs.python.org/3/library/textwrap.html#textwrap.dedent) to do what is needed. If your question contained a [mre], showing how to do this could be provided… – martineau Sep 27 '20 at 16:06

1 Answers1

0

I see couple of major logical issues with the code you shared.

  1. All conditions are stated as = instead of ==
        # For Example,
        if k=0 :   # This is wrong
        if k == 0: # This is correct for the comparison
    
  2. I am not seeing proper indentation in your code. Hopefully its copy paste issue.
  3. For your logic you can create an excellent function pointers map. For example
    def func1():
      print('func1')
    
    def func2():
      print('func2')
    
    funcs_map = {}
    funcs_map[1] = func1
    funcs_map[2] = func2
    
    
    for i in range(10);
      if i in funcs_map.keys():
          func_ptr = funcs_map[i]
          func_ptr()
    
    # The above approach would be easy to manage to it will help you debug
    # instead of hardcoding functionality within string. 
    

Good luck and see if above suggested fixed your issue.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Riyas
  • 268
  • 2
  • 11