##// END OF EJS Templates
Fix formatting of usage examples for `set_env` magic (#13911)...
Fix formatting of usage examples for `set_env` magic (#13911) See the difference between how `env` and `set_env` look in the [docs](http://ipython.readthedocs.io/en/stable/interactive/magics.html). <img width="600" alt="image" src="https://user-images.githubusercontent.com/14007150/215095416-c8e08aca-4c61-4bc9-a84e-81209607c74f.png"> <img width="725" alt="image" src="https://user-images.githubusercontent.com/14007150/215095718-532fe30f-a4b0-446e-ba61-73c5cf1e988e.png">

File last commit:

r27747:f5d4e0ac
r28081:13f20b59 merge
Show More
contexts.py
61 lines | 1.6 KiB | text/x-python | PythonLexer
Bradley M. Froehle
Better handling of `__file__` when running scripts....
r8529 # encoding: utf-8
Thomas Kluyver
Move most readline code from InteractiveShell to terminal subclass...
r21846 """Miscellaneous context managers.
Bradley M. Froehle
Better handling of `__file__` when running scripts....
r8529 """
Matthias Bussonnier
deprecated noop context managers
r22630 import warnings
Thomas Kluyver
Move most readline code from InteractiveShell to terminal subclass...
r21846 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Bradley M. Froehle
Better handling of `__file__` when running scripts....
r8529
Matthias Bussonnier
MAINT: run black on files that ends up in a single line change....
r27747
Bradley M. Froehle
Better handling of `__file__` when running scripts....
r8529 class preserve_keys(object):
"""Preserve a set of keys in a dictionary.
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.
Thomas Kluyver
Improvements to docs formatting.
r12553 Examples
--------
Bradley M. Froehle
Better handling of `__file__` when running scripts....
r8529
>>> 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()))
...
[('c', None), ('d', 4), ('e', 5)]
>>> print(sorted(d.items()))
[('b', 2), ('c', 3), ('e', 5)]
"""
def __init__(self, dictionary, *keys):
self.dictionary = dictionary
self.keys = keys
def __enter__(self):
# Actions to perform upon exiting.
to_delete = []
to_update = {}
d = self.dictionary
for k in self.keys:
if k in d:
to_update[k] = d[k]
else:
to_delete.append(k)
self.to_delete = to_delete
self.to_update = to_update
def __exit__(self, *exc_info):
d = self.dictionary
for k in self.to_delete:
d.pop(k, None)
d.update(self.to_update)