Module:utils.contexts

Miscellaneous context managers.

1 Class

classIPython.utils.contexts.preserve_keys(dictionary,*keys)

Bases:object

Preserve a set of keys in a dictionary.

Upon entering the context manager the current values of the keyswill be saved. Upon exiting, the dictionary will be updated torestore the original value of the preserved keys. Preserved keyswhich did not exist when entering the context manager will bedeleted.

Examples

>>>d={'a':1,'b':2,'c':3}>>>withpreserve_keys(d,'b','c','d'):...deld['a']...deld['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()))...[('c', None), ('d', 4), ('e', 5)]>>>print(sorted(d.items()))[('b', 2), ('c', 3), ('e', 5)]
__init__(dictionary,*keys)