首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python脚本逻辑或模块,可帮助编写独立执行少量活动步骤或阶段的主函数

Python脚本逻辑或模块,可帮助编写独立执行少量活动步骤或阶段的主函数
EN

Stack Overflow用户
提问于 2017-12-26 20:32:09
回答 1查看 151关注 0票数 0

我也是python和脚本编程的新手。我想要理解并编写一个python脚本来开始处理这个需求的逻辑。

我的main函数在步骤中执行一系列活动,假设step-1、step-2、step-3和step-4彼此独立。

通常,使用一些IF条件并按顺序定义这些步骤来执行将是很好的开始。但是,当我们在第-3步出现故障时,在修复它并再次重新运行脚本时,控制转到第-1步&以相同的顺序执行所有步骤。

但我的要求是,在step-3失败并重新运行修复的脚本时,无论是否使用某些命令行参数,控件都应该通过跳过step-1和step-2开始执行step-3,因为它们已经成功执行。我们在python中有没有任何模块可以帮助逻辑来实现这一点。

EN

回答 1

Stack Overflow用户

发布于 2018-01-22 23:00:52

@eiram_mahera,

感谢你分享的链接。

但在我的例子中,pickle可能不是更好的选择,因为我的脚本将来必须与groovy通信。我读到pickle不支持其他脚本。我目前正在编写psudo代码。我使用了config解析器和arg解析器来实现这个功能。将每个函数的输出存储到ini文件中,然后重新读取。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47978714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档