Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Context manager to save/restore a name binding

Reply
Thread Tools

Re: Context manager to save/restore a name binding

 
 
Peter Otten
Guest
Posts: n/a
 
      08-31-2012
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.
>
> aram namespace: The namespace object (e.g. a class or dict)
> containing the name binding.
> aram 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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Context manager to save/restore a name binding Chris Withers Python 0 08-31-2012 11:06 AM
HttpContext.Current.User.Identity.Name AND Context.User.Identity.Name; nalbayo ASP .Net 2 11-11-2005 11:12 PM
Cisco CW Campus Manager, CW Common Service, CW Device Fault Manager, CW Recource Manager Essentials, NGenious RealTime Monitor, CiscoWorks Routed WAN Management Solution v1.3 [3 CDs], CiscoWorks VPN_Security Management Solution v2.2, CiscoWorks QoS P astra35 Cisco 0 05-19-2004 01:01 PM
Cisco CW Campus Manager, CW Common Service, CW Device Fault Manager, CWRecource Manager Essentials, NGenious RealTime Monitor, CiscoWorksRouted WAN Management Solution v1.3 [3 CDs], CiscoWorks VPN_SecurityManagement Solution v2.2, CiscoWorks QoS Poli TEL Cisco 0 01-17-2004 07:09 AM
Cisco CW Campus Manager, CW Common Service, CW Device Fault Manager, CWRecource Manager Essentials, NGenious RealTime Monitor - new ! TEL Cisco 0 12-31-2003 07:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57