You are viewing an older version (9.11.0). Go to latest (9.13.0)
{ } Raw JSON

bundles / IPython 9.11.0 / IPython / utils / contexts / preserve_keys

class

IPython.utils.contexts:preserve_keys

source: /IPython/utils/contexts.py :14

Signature

class   preserve_keys ( dictionary : dict[Any, Any] * keys : Any )  →  None

Members

Summary

Preserve a set of keys in a dictionary.

Extended Summary

Upon entering the context manager the current values of the keys will be saved. Upon exiting, the dictionary will be updated to restore the original value of the preserved keys. Preserved keys which did not exist when entering the context manager will be deleted.

Examples

d = {'a': 1, 'b': 2, 'c': 3}
with preserve_keys(d, 'b', 'c', 'd'):
    del d['a']
    del d['b']      # will be reset to 2
    d['c'] = None   # will be reset to 3
    d['d'] = 4      # will be deleted
    d['e'] = 5
    print(sorted(d.items()))
print(sorted(d.items()))
[('b', 2), ('c', 3), ('e', 5)]

Aliases

  • IPython.core.shellapp.preserve_keys