@contextmanager
def environment_as(**kwargs):
 new_environment = kwargs
 old_environment = {}
 def setenv(key, val):
  if (val is not None):
   os.environ[key] = val
  elif (key in os.environ):
   del os.environ[key]
 for (key, val) in new_environment.items():
  old_environment[key] = os.environ.get(key)
  setenv(key, val)
 try:
  (yield)
 finally:
  for (key, val) in old_environment.items():
   setenv(key, val)

@ contextmanager def environment_as ( ** kwargs ) : new_environment = kwargs old_environment = { } def setenv ( key , val ) : if ( val is not None ) : os . environ [ key ] = val elif ( key in os . environ ) : del os . environ [ key ] for ( key , val ) in new_environment . items ( ) : old_environment [ key ] = os . environ . get ( key ) setenv ( key , val ) try : ( yield ) finally : for ( key , val ) in old_environment . items ( ) : setenv ( key , val )
update the environment to the supplied values .