Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tmpdir and tmpfile context manager docstrings #8270

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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