Author Julian
Recipients Julian, eric.snow, giampaolo.rodola, nikratio, rhettinger, smarnach
Date 2011-12-13.00:23:25
SpamBayes Score 6.54845e-06
Marked as misclassified No
Message-id <1323735806.33.0.255782076719.issue13585@psf.upfronthosting.co.za>
In-reply-to
Content
For reference, the implementation that I posted in the other thread is:

    @contextlib.contextmanager
    def maybe(got, contextfactory, *args, checkif=bool, **kwargs):
        if checkif(got):
            yield got
        else:
            with contextfactory(*args, **kwargs) as got:
                yield got

which produces code like:

def fn(input_file=None):
    with maybe(input_file, open, "/default/input/file/location/"):
        for line in input_file:
            print line

For the example given here in the OP, since rmtree isn't a contextmanager it'd require wrapping it in one first.

I could probably understand if there was desire for these to be a recipe instead. The advantage (if any) of the above over this class is that it keeps context managers sorta looking like context managers. Here if you wanted to write my example it'd require writing code like

with ContextManager() as mgr:
    foo = ctxtmgr()
    mgr.register(foo.__close__)

which doesn't look too great :/

The class though does have wider scope though, since it doesn't necessarily only need a context manager, it can do cleanup for anything, which I guess is nice.
History
Date User Action Args
2011-12-13 00:23:26Juliansetrecipients: + Julian, rhettinger, giampaolo.rodola, nikratio, eric.snow, smarnach
2011-12-13 00:23:26Juliansetmessageid: <1323735806.33.0.255782076719.issue13585@psf.upfronthosting.co.za>
2011-12-13 00:23:25Julianlinkissue13585 messages
2011-12-13 00:23:25Juliancreate