added dummy swap

This commit is contained in:
Anthony Scopatz 2017-01-08 20:23:07 -05:00
parent 8cf955ecb5
commit 264c48c0b1

View file

@ -7,6 +7,7 @@ import ast
import builtins import builtins
import platform import platform
import subprocess import subprocess
import contextlib
from collections import defaultdict from collections import defaultdict
from collections.abc import MutableMapping from collections.abc import MutableMapping
@ -106,6 +107,27 @@ class DummyEnv(MutableMapping):
def __iter__(self): def __iter__(self):
yield from self._d yield from self._d
@contextlib.contextmanager
def swap(self, other=None, **kwargs):
old = {}
# single positional argument should be a dict-like object
if other is not None:
for k, v in other.items():
old[k] = self.get(k, NotImplemented)
self[k] = v
# kwargs could also have been sent in
for k, v in kwargs.items():
old[k] = self.get(k, NotImplemented)
self[k] = v
yield self
# restore the values
for k, v in old.items():
if v is NotImplemented:
del self[k]
else:
self[k] = v
# #
# Execer tools # Execer tools
# #