Skip to content

Commit

Permalink
Update tmpdir and tmpfile context manager docstrings (#8270)
Browse files Browse the repository at this point in the history
Co-authored-by: James Bourbeau <jrbourbeau@users.noreply.github.com>
  • Loading branch information
mesejo and jrbourbeau committed Oct 20, 2021
1 parent 5b63301 commit f424669
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions dask/utils.py
Expand Up @@ -165,6 +165,31 @@ def import_required(mod_name, error_msg):

@contextmanager
def tmpfile(extension="", dir=None):
"""
Function to create and return a unique temporary file with the given extension, if provided.
Parameters
----------
extension : str
The extension of the temporary file to be created
dir : str
If ``dir`` is not None, the file will be created in that directory; otherwise,
Python's default temporary directory is used.
Returns
-------
out : str
Path to the temporary file
See Also
--------
NamedTemporaryFile : Built-in alternative for creating temporary files
tmp_path : pytest fixture for creating a temporary directory unique to the test invocation
Notes
-----
This context manager is particularly useful on Windows for opening temporary files multiple times.
"""
extension = "." + extension.lstrip(".")
handle, filename = tempfile.mkstemp(extension, dir=dir)
os.close(handle)
Expand All @@ -183,6 +208,24 @@ def tmpfile(extension="", dir=None):

@contextmanager
def tmpdir(dir=None):
"""
Function to create and return a unique temporary directory.
Parameters
----------
dir : str
If ``dir`` is not None, the directory will be created in that directory; otherwise,
Python's default temporary directory is used.
Returns
-------
out : str
Path to the temporary directory
Notes
-----
This context manager is particularly useful on Windows for opening temporary directories multiple times.
"""
dirname = tempfile.mkdtemp(dir=dir)

try:
Expand Down

0 comments on commit f424669

Please sign in to comment.