从停止的位置启动python执行流

2024-10-02 00:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下两个输入文件示例:

case1:

one-device:yes
number of device:01-05
first-device:
second-device:

Case2:
one-device:no
number of device:01-05
first-device:01-02
second-device:03-03
third-device:04-05

现在在案例1中,我只有一个开始和结束值,即01和05

我的函数是func1到func13。我举了两个例子

def func1(self, start, end):
     for i, x in enumerate (range(start, end)):
         do something
def func10 (self, start, end):
         do something

if one-device == yes: # execute func1 one time. 
    func1(arg1, agr2)

# if one-device no i need to run 3 times 
if one-device == no:
    for x in (1, 4)
        func1(star, end) # just example start and end values will changes as per inputs in this example i just defined them start end value
func2(arg1, agr2)
func3(arg1, agr2)
func4(arg1, agr2)
func5(arg1, agr2)
func6(arg1, agr2)
func7(arg1, agr2)
func8(arg1)
func9(arg1, agr2,arg3)
func10(agr1,arg2)
func11(arg1, agr2)
func12(arg1, agr2)
func13(arg1) 

现在在case2输入示例中,由于某些原因程序执行被停止。你知道吗

它被执行了一次func1并停止了。你知道吗

现在我需要为用户提供一个选项,从停止的地方开始执行。你知道吗

用户知道它在哪里被阻止了。你知道吗

我将提供如下选项:

list1 = ['func1','func2','func3',.......'func13'] # list1 example

for x, y in zip(range(1, 14), list1):
    print "{}. {}".format(x, y)
input_select = raw_input("choose options")

if input_select == '1':
    list2 = ['first_device_func1', 'second_device_func1', 'third_device_func1']
    for  x, y in zip(range(1, 4), list2):
        print "{}. {}".format(x, y)
    input_select_func1 = raw_input("Choose fun from it will start")

在上面的示例中,用户选择了func1,然后再次选择了2。你知道吗

现在我的程序必须从用户输入开始执行。你知道吗

请帮忙,因为之前我把一切都搞糊涂了,所以我带着输入和输出来了。希望这一次一切都会明朗。你知道吗


Tags: 用户in示例forinputifdeviceone
1条回答
网友
1楼 · 发布于 2024-10-02 00:21:29

因为您的函数有不同数量的参数,所以我会将它们包装在^{}对象中,然后构造某种结构来访问它们:

from functools import partial

def func1(.....):
    ...

def func2(.....):
    ...

functions = [
    [partial(func1, arg1, arg2), partial(func1, arg3, arg4)],
    partial(func2, arg1),
    ...
]

def print_function_list(func_list, prepend=''):
    for i, item in enumerate(func_list, start=1):
        order_signifier = "{}{}.".format(prepend, i)
        if isinstance(item, list):
            print_function_list(item, prepend=order_signifier)
        else:
            print("{:<6} {}".format(order_signifier, item.func.__name__))

def start_at():
    print("Available functions:")
    print_function_list(functions)
    choice = raw_input("Choose: ")
    return [int(i) for i in choice.split('.')]

def run_functions(start_pos, function_list):
    for i, item in enumerate(function_list, start=1):
        if i < start_pos[0]:
            continue
        if isinstance(item, list):
            if i == start_pos[0]:
                start = start_pos[1:]
            else:
                start = [1]
            run_functions(start, item)
        else:
            print("Running {}".format(item.func.__name__))
            item()

run_functions(start_at(), functions)

这缺乏对输入的错误检测,总体上可以做得更好,但应该足以让您开始。你知道吗

相关问题 更多 >

    热门问题