Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28157

Context manager to save/restore a name binding

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.alt.net!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Context manager to save/restore a name binding
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
Date Fri, 31 Aug 2012 12:36:29 +1000
Message-ID <87oblrbxea.fsf@benfinney.id.au> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:uOh3SJMuKaBYNJtX+oYVPXdxDNQ=
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Lines 44
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host 5741eb56.news.astraweb.com
X-Trace DXC=QNabf_b8;H0hZ4>`7BGPo4L?0kYOcDh@:7^o:UA4R?c5m8]Ha;Fb[[:]G;2>V^?kW3bEW9A[5UK?5NZ[SL`C\Kg3oU>:fe0O\g6
Xref csiph.com comp.lang.python:28157

Show key headers only | View raw


Howdy all,

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?

-- 
 \       “When a well-packaged web of lies has been sold to the masses |
  `\    over generations, the truth will seem utterly preposterous and |
_o__)                    its speaker a raving lunatic.” —Dresden James |
Ben Finney

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Context manager to save/restore a name binding Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 12:36 +1000
  Re: Context manager to save/restore a name binding Peter Otten <__peter__@web.de> - 2012-08-31 08:27 +0200
    Re: Context manager to save/restore a name binding Ben Finney <ben+python@benfinney.id.au> - 2012-08-31 19:32 +1000
  Re: Context manager to save/restore a name binding Chris Withers <chris@python.org> - 2012-08-31 12:06 +0100

csiph-web