I am trying to hide some try/except complexity with a contextmanager. Here is an easy example:
from contextlib import contextmanager
import mpd
mpdclient = mpd.MPDClient()
mpdclient.connect("localhost", 6600)
@contextmanager
def mpdcontext():
try:
yield
except mpd.ConnectionError:
mpdclient.connect("localhost", 6600)
with mpdcontext():
mpdclient.status()
with mpdcontext():
mpdclient.lsinfo()
Now, as I understood, the block in the with statement is executed when yield is called. In my case, if this raises an exception, I reconnect to mpd. Can I somehow execute the with-block again after this reconnect?
Thanks