0

I am new to python and scripting as well. I wanted to understand and write a python script to start working out with the logic for the this requirement.

My main function executes series of activities in steps, lets say step-1, step-2, step-3 & step-4 independent of each other.

Normally, using some IF condition and defining these steps in sequential order to execute would be good to start with. But, when we have failure at step-3 and on fixing it and re-running the script again control goes to step-1 & executes all the steps in the same sequence.

But my requirement is that, on step-3 failure and re-running the fixed script with or without some command line parameter the control should start executing step-3 by skipping step-1& step-2 as they already got executed successfully. Do we have any module in python that helps & logic to achieve this.

Raghu
  • 21
  • 3
  • Read this thread: https://stackoverflow.com/questions/5568904/saving-the-state-of-a-program-to-allow-it-to-be-resumed. – eiram_mahera Dec 26 '17 at 13:28

1 Answers1

0

@eiram_mahera,

thanks for the link shared.

But in my case pickle might not be the better option as my script has to communicate to groovy in future. I read that pickle won't support other scripts. I am currently working on psudo code. I used config parser & arg parser to bring this functionality. Storing output of each function to ini file and reading again from it.

import sys
import os
import math
import alert
import ConfigParser
import argparse
from alert import alert_user

configParser = ConfigParser.ConfigParser()
configParser.read('sample_updated.ini')


def function_1(a, b):
    c = a + b
    # write to ini file
    configParser.set('Output', 'function_1', str(c))
    print "input_1 of function_1: ", a
    print "input_2 of function_1: ", b
    print "output of function_1: ", c
    return c


def function_2(c, b):
    d = c * b
    # write to ini file
    configParser.set('Output', 'function_2', str(d))
    print "input_1 of function_2: ", c
    print "input_2 of function_2: ", b
    print "output of function_2: ", d
    return d


def function_3(d, c):
    e = d / c
    # write to ini file
    configParser.set('Output', 'function_3', str(e))
    print "input_1 of function_3: ", d
    print "input_2 of function_3: ", c
    print "output of function_3: ", e
    return e


def function_4(d, e):
    f = d - e
    # write to ini file
    configParser.set('Output', 'function_4', str(f))
    print "input_1 of function_4: ", d
    print "input_2 of function_4: ", e
    print "output of function_4: ", f
    return f


if __name__ == '__main__':

    # alert as a sound
    # continue_release = alert_user("shall we proceed further? Y/N: ")
    #
    # if continue_release == "Y" or continue_release == "y":

        parser = argparse.ArgumentParser()
        # selection = []
        # create argument parsers
        parser.add_argument('-function_2', action='store_true', default=None, dest='function_2', help="execute from function_2")

        parser.add_argument('-function_3', action='store_true', default=None, dest='function_3', help="execution starts from function_3")

        parser.add_argument('-function_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'), configParser.getint('Input', 'value_of_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'))

        if cmd_arguments.function_3:
            function_3(configParser.getint('Output', 'function_2'), configParser.getint('Output', 'function_1'))
            function_4(configParser.getint('Output', 'function_2'), configParser.getint('Output', 'function_3'))

        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(configParser.getint('Input', 'value_of_a'), configParser.getint('Input', 'value_of_b'))
            function_2(configParser.getint('Output', 'function_1'), configParser.getint('Input', 'value_of_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)
Raghu
  • 21
  • 3
  • Can you help me making this script better. – Raghu Jan 22 '18 at 15:01
  • I have only 4 functions here, so it is easy to call all the consecutive functions in each "if" - statement. But when working with more functions somewhere around 10 and above, this could be tedious to call all the functions like above. Is there another way we can call those consecutive functions in simple manner than above? – Raghu Jan 23 '18 at 09:50