contexts.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # encoding: utf-8
  2. """Miscellaneous context managers.
  3. """
  4. import warnings
  5. # Copyright (c) IPython Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. class preserve_keys(object):
  8. """Preserve a set of keys in a dictionary.
  9. Upon entering the context manager the current values of the keys
  10. will be saved. Upon exiting, the dictionary will be updated to
  11. restore the original value of the preserved keys. Preserved keys
  12. which did not exist when entering the context manager will be
  13. deleted.
  14. Examples
  15. --------
  16. >>> d = {'a': 1, 'b': 2, 'c': 3}
  17. >>> with preserve_keys(d, 'b', 'c', 'd'):
  18. ... del d['a']
  19. ... del d['b'] # will be reset to 2
  20. ... d['c'] = None # will be reset to 3
  21. ... d['d'] = 4 # will be deleted
  22. ... d['e'] = 5
  23. ... print(sorted(d.items()))
  24. ...
  25. [('c', None), ('d', 4), ('e', 5)]
  26. >>> print(sorted(d.items()))
  27. [('b', 2), ('c', 3), ('e', 5)]
  28. """
  29. def __init__(self, dictionary, *keys):
  30. self.dictionary = dictionary
  31. self.keys = keys
  32. def __enter__(self):
  33. # Actions to perform upon exiting.
  34. to_delete = []
  35. to_update = {}
  36. d = self.dictionary
  37. for k in self.keys:
  38. if k in d:
  39. to_update[k] = d[k]
  40. else:
  41. to_delete.append(k)
  42. self.to_delete = to_delete
  43. self.to_update = to_update
  44. def __exit__(self, *exc_info):
  45. d = self.dictionary
  46. for k in self.to_delete:
  47. d.pop(k, None)
  48. d.update(self.to_update)