I tried this:
@contextmanager
def changed_dir(dirname, msg="nothing to do.."):
if changed(dirname):
yield
else:
print msg
return
however when I tried to use it:
with changed_dir("foo/bar"):
print "something in foo/bar was changed"
I was greeted with:
RuntimeError: generator didn't yield
is there any way to get this to work?
update: many people seem to get stuck on the simplicity of the example. Here is a more complex example illustrating the same point
@contextmanager
def changed_dir(dirname, msg="..."):
cn = db.get_connection(...)
try:
cn.execute("insert ...")
if changed(dirname):
cn.execute(...)
yield
os.mkdirs(os.path.join('backup', dirname))
# copy the modified tree..
# etc.
else:
cn.execute(...)
print msg
cn.commit()
except:
cn.rollback()
# cleanup from mkdirs call...
finally:
cn.close()
is in-lining the above still the only solution?