Jan-31-2018, 07:12 AM
Do we have any options in argument parser or any other way for continuing the execution from that respective position till the end of the code, on providing a command-line argument? Also the other way of executing only that function.
I have a simple script as below. When I execute with "script.py -f_2" it does execute only function_2. But I want to execute all the subsequent functions function_3 & function_4 as well till the end. How can I achieve both functionality like executing only that function and sometimes along with subsequent functions?
This script also read's and writes content from a file called sample_update.ini using config parser.
I have a simple script as below. When I execute with "script.py -f_2" it does execute only function_2. But I want to execute all the subsequent functions function_3 & function_4 as well till the end. How can I achieve both functionality like executing only that function and sometimes along with subsequent functions?
This script also read's and writes content from a file called sample_update.ini using config parser.
import sys import os import math import alert import ConfigParser import argparse from alert import alert_user configParser = ConfigParser.ConfigParser() configParser.add_section('Input') configParser.add_section('Output') configParser.read('sample_updated.ini') def function_1(a, b): c = a + b # write to ini file print "input_1 of function_1: ", a print "input_2 of function_1: ", b print "output of function_1: ", c configParser.set('Input', 'function_1_1', str(a)) configParser.set('Input', 'function_1_2', str(b)) configParser.set('Output', 'function_1', str(c)) return c def function_2(c, b): d = c * b # write to ini file print "input_1 of function_2: ", c print "input_2 of function_2: ", b print "output of function_2: ", d configParser.set('Output', 'function_2', str(d)) return d def function_3(d, c): e = d / c # write to ini file print "input_1 of function_3: ", d print "input_2 of function_3: ", c print "output of function_3: ", e configParser.set('Output', 'function_3', str(e)) return e def function_4(d, e): f = d - e # write to ini file print "input_1 of function_4: ", d print "input_2 of function_4: ", e print "output of function_4: ", f configParser.set('Output', 'function_4', str(f)) return f if __name__ == '__main__': a = input("value of a: ") b = input("value of b: ") parser = argparse.ArgumentParser() # create argument parser parser.add_argument('-f_2', action='store_true', default=None, dest='function_2', help="execute from function_2") parser.add_argument('-f_3', action='store_true', default=None, dest='function_3', help="execution starts from function_3") parser.add_argument('-f_4', action='store_true', default=None, dest='function_4', help="execution starts from function_4") cmd_arguments = parser.parse_args() # print selected functions if cmd_arguments.function_2: function_2(configParser.getint('Output', 'function_1'), b) if cmd_arguments.function_3: function_3(configParser.getint('Output', 'function_2'), if cmd_arguments.function_4: function_4(configParser.getint('Output', 'function_2'), configParser.getint('Output', 'function_3')) if not len(sys.argv) > 1: # store function outputs function_1(a, b) function_2(configParser.getint('Output', 'function_1'), b) function_3(configParser.getint('Output', 'function_2'), configParser.getint('Output', 'function_1')) function_4(configParser.getint('Output', 'function_2'), configParser.getint('Output', 'function_3')) with open('sample_updated.ini', 'w') as configfile: configParser.write(configfile)