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


Groups > comp.lang.python > #28160

Re: Context manager to save/restore a name binding

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"""': 0.05; 'context': 0.05; 'finally:': 0.05; 'try:': 0.07; '8bit%:30': 0.09; 'doing?': 0.09; 'name)': 0.09; 'name):': 0.09; 'namespace': 0.09; 'none.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'restored': 0.09; 'saved.': 0.09; 'def': 0.10; 'value.': 0.15; '`name`': 0.16; 'binding.': 0.16; 'contextlib': 0.16; 'exists?': 0.16; 'exited,': 0.16; 'finney': 0.16; 'object()': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'code,': 0.18; 'module': 0.19; 'written': 0.20; 'import': 0.21; 'specified': 0.23; 'header:User- Agent:1': 0.26; 'setting': 0.26; '(e.g.': 0.27; 'header:X -Complaints-To:1': 0.28; 'subject:/': 0.28; 'email name:': 0.29; 'value)': 0.29; 'wrap': 0.29; 'class': 0.29; 'code': 0.31; "skip:' 20": 0.32; 'to:addr:python-list': 0.33; 'whatever': 0.35; 'ben': 0.35; 'saved': 0.35; 'there': 0.35; 'received:org': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'previous': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'save': 0.61; 'containing': 0.61; '8bit%:21': 0.69; 'restore': 0.69; 'url:a': 0.72; 'touched': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Context manager to save/restore a name binding
Date Fri, 31 Aug 2012 08:27:13 +0200
Organization None
References <87oblrbxea.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Gmane-NNTP-Posting-Host p5084a31d.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4000.1346395794.4697.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346395794 news.xs4all.nl 6855 [2001:888:2000:d::a6]:51812
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:28160

Show key headers only | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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