@contextlib.contextmanager
def use_context_with_mode(context, mode):
 assert (mode in (Context.BEHAVE, Context.USER))
 current_mode = context._mode
 try:
  context._mode = mode
  (yield)
 finally:
  context._mode = current_mode

@ contextlib . contextmanager def use_context_with_mode ( context , mode ) : assert ( mode in ( Context . BEHAVE , Context . USER ) ) current_mode = context . _mode try : context . _mode = mode ( yield ) finally : context . _mode = current_mode
switch context to behave or user mode .