SHARE
TWEET

mlk

a guest Aug 9th, 2008 174 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from contextlib import contextmanager
  3.  
  4. def adapter(ob, fenter, fexit):
  5.     nop = lambda: None
  6.     on_enter = getattr(ob, fenter, nop)
  7.     on_exit = getattr(ob, fexit, nop)
  8.     @contextmanager
  9.     def manager():
  10.         on_enter()
  11.         try:
  12.             yield
  13.         finally:
  14.             on_exit()
  15.     return manager()
RAW Paste Data
Top