i looking try figure out way write this:
code_that_should_not_be_run_again() rerun_code_that_may_fail(): another_method(x) run_me_again_if_i_fail(y) code_that_should_only_be_run_if_above_succeeds()
where above run code, , catch exception, if caught, try run code, again. here longer version of want:
code_that_should_not_be_run_again() try: another_method(x) run_me_again_if_i_fail(y) catch exception: try: another_method(x) run_me_again_if_i_fail(y) catch exception: raise exception("couldn't run") code_that_should_only_be_run_if_above_succeeds()
i thinking use generator, maybe catch yielded content in lambda , run twice, somehow, sure how can code this.
is possible in python? or maybe similar can done?
here's i've tried far:
from contextlib import contextmanager @contextmanager def run_code(): print 'will run' try: yield except someexception: try: yield except someexception: raise someexception('couldn't run')
edit python wont' let want do, can use decorators on functions :(
using retry decorator - https://pypi.python.org/pypi/retry/ - , under scenario want catch typeerror
, maximum of 3 tries, delay of 5 seconds:
from retry import retry @retry(typeerror, tries=3, delay=5) def code_block_that_may_fail(): method_1() method_2() #some more methods here... code_block_that_may_fail()
can't cleaner that.
additionally, can use built-in logger log failed attempts (see documentation).
Comments
Post a Comment