简体   繁体   中英

Start python execution flow from where it got stopped

I have below two input file examples:

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

Now in case 1 i have only one start and end value that is 01 and 05

Functions I have is func1 to func13 . I have provided two examples

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) 

Now program execution is stopped for some reasons in case2 input example .

It got executed one time func1 and stopped.

Now I need to provide option to user to start execution from where it got stopped.

Users knows where it got stopped.

I will provide options like this:

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")

In above example user selected func1 , and then again he selected 2 .

Now my program execution must be start from user input.

Please help, as previous I have confused all so I came with input and output expected. Hope this time it will be clear.

Since your functions have varying number of arguments, I would wrap them in functools.partial objects and then construct some sort of structure to access them:

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)

This lacks error detection of the input and could be done a lot better as a whole, but it should be enough to get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM