(Please keep discussion on the list) Thanks for your critique Sagiv.
On Thu, Jan 6, 2011 at 1:06 PM, Sagiv Malihi <[email protected]> wrote: > plus - I think in python3.2 @contextlib.contextmanager already returns a > ContextDecorator .... > Yes it does; With my `ContextManager` we enjoy the benefits of `ContextDecorator` *in addition* to all the other features of `ContextManager`. > > > On Thu, Jan 6, 2011 at 1:05 PM, Sagiv Malihi <[email protected]>wrote: > >> I'm not sure what the manage_context is for()... >> Did you mean to do something along the lines of: >> >> def nest_with(managing_contextmanager): >> def decorator(managed_contextmanager): >> return lambda *args,**kwargs: nested(managing_contextmanager(), >> managed_contextmanager(*args, **kwargs)) >> return decorator >> >> so that you can 'auto nest' another contextmanager outside yours? >> >> e.g. >> >> @contextmanager >> def outer(): >> print "before" >> yield >> print "after" >> >> >> @nest_with(outer) >> @contextmanager >> def inner(): >> print "in" >> yield >> print "out" >> >> @nest_with(outer) >> class MyContextManager(object): >> def __enter__(self): >> print "in" >> def __exit__(self, et, ev ,tb): >> print "out" >> >> with inner(): >> print "middle" >> >> with MyContextManager(): >> print "middle" >> >> -- Sagiv >> > `nest_with` is only for the specific case where you want to nest one context manger inside another. `manage_context` is more general: you can use it for nesting, but you can do whatever else you want with it. Also, even for nesting, `nest_with` has some problems. For example in my project I have this: class HistoryBrowser(garlicsim.misc.BaseHistoryBrowser, ContextManager): # ... def manage_context(self): with self.tree_lock.read: yield self I couldn't use `nest_with` on this one, because `self.tree_lock.read` doesn't exist yet when the module is imported. Also I would say that the use of `nest_with` is easier to miss by the reader than a `manage_context` method. Ram.
_______________________________________________ Python-il mailing list [email protected] http://hamakor.org.il/cgi-bin/mailman/listinfo/python-il
