I've been writing some python app and unfortunately I stumbled upon (I believe...) a Design Pattern/Flow Control problem.
Let's say that I have some big function that includes multiple other function and outcome of the big function is strictly determined by small function success.
So the big function simply contains a series of operations.
If just one small function fails then we should abort the big function, unfortunately the deeper we go... then the small functions make more modifications to some objects, so I think that I just cannot abort the big function, because I have to revert/repair some of those unfinished changes made by small functions - "perform cleanup".
Is there a pythonic way to check/control execution of small functions inside a big function? Because the solution that I have now seems extremely ugly and not very ZENish...
Here is some pseudo code that represents the solution that I have now:
def small_func():
try:
# doing something here
return True # if operation successful
except Error:
return False
def big_func():
# state var helping determine if we need to cleanup if some of the
# funtions were unsuccessful
state = True
if not small_func1():
state = False
if not small_func2():
state = False
if not small_func3():
state = False
if not small_func4():
state = False
if not small_func4():
state = False
etc...
if not state:
#perform some cleanup after failed function since we can't
#finish the big function - we need to abort and clean unfinished stuff