##// END OF EJS Templates
ipython_directive: Adjust doc examples for reproducibility....
ipython_directive: Adjust doc examples for reproducibility. Before this change, building the documentation twice in a row in an environment configured for reproducible bulids would result in discrepancies such as: diff -ru /gnu/store/...-python-ipython-documentation-8.2.0/share/doc/python-ipython-documentation-8.2.0/html/sphinxext.html /gnu/store/...-python-ipython-documentation-8.2.0-check/share/doc/python-ipython-documentation-8.2.0/html/sphinxext.html --- /gnu/store/...-python-ipython-documentation-8.2.0/share/doc/python-ipython-documentation-8.2.0/html/sphinxext.html 1969-12-31 19:00:01.000000000 -0500 +++ /gnu/store/...-python-ipython-documentation-8.2.0-check/share/doc/python-ipython-documentation-8.2.0/html/sphinxext.html 1969-12-31 19:00:01.000000000 -0500 @@ -682,7 +682,7 @@ <span class="gp">In [2]: </span><span class="kn">import</span> <span class="nn">datetime</span> <span class="gp"> ...: </span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="gp"> ...: </span> -<span class="gh">Out[2]: </span><span class="go">datetime.datetime(2022, 4, 17, 3, 21, 14, 978155)</span> +<span class="gh">Out[2]: </span><span class="go">datetime.datetime(2022, 4, 17, 3, 37, 37, 115081)</span> </pre></div> </div> <p>It supports IPython construct that plain @@ -690,7 +690,7 @@ <div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="kn">import</span> <span class="nn">time</span> <span class="gp">In [4]: </span><span class="o">%</span><span class="k">timeit</span> time.sleep(0.05) -<span class="go">50.2 ms +- 104 us per loop (mean +- std. dev. of 7 runs, 10 loops each)</span> +<span class="go">50.1 ms +- 8.86 us per loop (mean +- std. dev. of 7 runs, 10 loops each)</span> </pre></div> </div> <p>This will also support top-level async when using IPython 7.0+</p> * IPython/sphinxext/ipython_directive.py: Use a fixed date string in the datetime example, and replace the %timeit example by %pdoc, whole output is static.

File last commit:

r27548:6d40fd89 merge
r27687:71d665c4
Show More
tempdir.py
58 lines | 1.8 KiB | text/x-python | PythonLexer
""" This module contains classes - NamedFileInTemporaryDirectory, TemporaryWorkingDirectory.
These classes add extra features such as creating a named file in temporary directory and
creating a context manager for the working directory which is also temporary.
"""
import os as _os
from pathlib import Path
from tempfile import TemporaryDirectory
class NamedFileInTemporaryDirectory(object):
def __init__(self, filename, mode="w+b", bufsize=-1, add_to_syspath=False, **kwds):
"""
Open a file named `filename` in a temporary directory.
This context manager is preferred over `NamedTemporaryFile` in
stdlib `tempfile` when one needs to reopen the file.
Arguments `mode` and `bufsize` are passed to `open`.
Rest of the arguments are passed to `TemporaryDirectory`.
"""
self._tmpdir = TemporaryDirectory(**kwds)
path = Path(self._tmpdir.name) / filename
encoding = None if "b" in mode else "utf-8"
self.file = open(path, mode, bufsize, encoding=encoding)
def cleanup(self):
self.file.close()
self._tmpdir.cleanup()
__del__ = cleanup
def __enter__(self):
return self.file
def __exit__(self, type, value, traceback):
self.cleanup()
class TemporaryWorkingDirectory(TemporaryDirectory):
"""
Creates a temporary directory and sets the cwd to that directory.
Automatically reverts to previous cwd upon cleanup.
Usage example:
with TemporaryWorkingDirectory() as tmpdir:
...
"""
def __enter__(self):
self.old_wd = Path.cwd()
_os.chdir(self.name)
return super(TemporaryWorkingDirectory, self).__enter__()
def __exit__(self, exc, value, tb):
_os.chdir(self.old_wd)
return super(TemporaryWorkingDirectory, self).__exit__(exc, value, tb)