-3

Sometimes when I run a function in my python script and if the function gives me an output which is not desired, I need to undo this step and then try running the function again with different parameters. Is there a way that I can have the script undo what it has done if my function gives me a wrong output.For only one set of parameters, the desired output is acheived.

PS: By running the function means making permanent changes.

Example:

def function():
    for i in range(parameters):
        value = perform_operation(parameters[i]) #makes permanent changes 
        if value != some_value:
            undo(perform_operation()) 
Abdul
  • 79
  • 2
  • 7
  • Can you give an example of such a function? If you are consistently (or often enough) getting wrong output then the function might be wrong. If that is not the case, simply check the correctness of your output with an `if` like so: `if not correct_output: your_function(new_input)`. Give an example of the output too, depending on what your output looks like undo-ing it is very different. – Lomtrur Aug 17 '17 at 07:51
  • 1
    What do you mean by "undo what it has done"? Maybe you can do it by a `try` - `except` clause, raising an exception inside `try` and undo the changes on `except` – Martin Alonso Aug 17 '17 at 07:52
  • Just use a conditional statement? `if something: value = perform_operation() elif something_else: value = whatevs ...` –  Aug 17 '17 at 08:13
  • Why don't you make changes on a copy and make them permanent if conditions are met? – zipa Aug 17 '17 at 08:19
  • @MrGrj I need to call perform_operation() initially, based on its return value I need to decide if this operation is satisfied or not. Because perform_operation() makes permanent changes I need to be more careful. – Abdul Aug 17 '17 at 08:27
  • 1
    There's nothing that does this automatically. If you need to undo things, your script needs to save the old values somewhere so it can put them back. – Barmar Aug 17 '17 at 08:59

1 Answers1

2

You need to create another function than cleans what was done in the main function for example (delete newly created files, remove installed packages, etc ...) and use it.

def main_func():
    proc = subprocess.Popen('touch test test2')
    return proc.returncode

def clean_main_func():
   subprocess.Popen('rm test test2')


def __name__ == '__main__':
    result = main_func()
    if main_func() != 0 :
       clean_main_func()

or you can raise an error then catch it:

def main_func():
    proc = subprocess.Popen('touch test test2')
    if proc.returncode !=0 :
       raise Error

def clean_main_func():
   subprocess.Popen('rm test test2')


def __name__ == '__main__':
    try:
        result = main_func()
    except Error:
        clean_main_func()

It's juste an example , and hope it answer your question.

slim tabka
  • 43
  • 9
  • This worked great for me and helped me rethink my design. Just one comment, I think @silmtabka did not mean to create a `__name__` function, but an if statement. e.g. `if __name__ == '__main__':` – CodeAddictJack Mar 28 '19 at 16:05