Re: Context manager to save/restore a name binding

Discussion in 'Python' started by Peter Otten, Aug 31, 2012.

  1. Peter Otten

    Peter Otten Guest

    Ben Finney wrote:

    > I have written a context manager to save and restore a name binding::
    >
    > import contextlib
    >
    > @contextlib.contextmanager
    > def preserve_value(namespace, name):
    > """ A context manager to preserve, then restore, the specified
    > binding.
    >
    > :param namespace: The namespace object (e.g. a class or dict)
    > containing the name binding.
    > :param name: The name of the binding to be preserved.
    > :yield: None.
    >
    > When the context manager is entered, the current value bound
    > to `name` in `namespace` is saved. When the context manager is
    > exited, the binding is re-established to the saved value.
    >
    > """
    > saved_value = getattr(namespace, name)
    > yield
    > setattr(namespace, name, saved_value)
    >
    > The use case is <URL: http://stackoverflow.com/a/6811921/70157>, where
    > it's used like this::
    >
    > with preserve_value(sys, 'dont_write_bytecode'):
    > sys.dont_write_bytecode = True
    > module = imp.load_module(…)
    >
    > That way, I can set ‘sys.dont_write_bytecode’ to the value I need in
    > this part of the code, knowing that however the code continues the
    > previous value of that setting will be restored to whatever it was
    > before I touched it.
    >
    > Have I re-invented a context manager which already exists? Is there a
    > better way to do what ‘preserve_value’ is doing?


    You should wrap yield in a try ... finally. You might allow setting the new
    value in the manager (untested):

    import contextlib
    missing = object()

    @contextlib.contextmanager
    def preserve_attr(namespace, name, value=missing):
    saved_value = getattr(namespace, name)
    if value is not missing:
    setattr(namespace, name, value)
    try:
    yield
    finally:
    setattr(namespace, name, saved_value)
    Peter Otten, Aug 31, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reinhard Vornholt

    Save Viewstate in DB and restore it later

    Reinhard Vornholt, Jul 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    509
    Reinhard Vornholt
    Jul 30, 2003
  2. 1388-2/HB
    Replies:
    0
    Views:
    363
    1388-2/HB
    Mar 1, 2007
  3. Carl K

    attribute save restore

    Carl K, Apr 13, 2007, in forum: Python
    Replies:
    4
    Views:
    280
    Bruno Desthuilliers
    Apr 13, 2007
  4. kj

    How to save/restore the call stack?

    kj, May 26, 2009, in forum: C Programming
    Replies:
    8
    Views:
    1,198
    Dik T. Winter
    Jun 2, 2009
  5. Chris Withers
    Replies:
    0
    Views:
    201
    Chris Withers
    Aug 31, 2012
Loading...

Share This Page