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

Use uuid.uuid4 to generate unique names #274

Merged
merged 5 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pygmt/helpers/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
Utilities for dealing with temporary file management.
"""
import os
import uuid
from tempfile import NamedTemporaryFile

import numpy as np


def unique_name():
"""
Generate a unique name with the prefix 'gmt-python-'.
Generate a unique name.

Useful for generating unique names for figures (otherwise GMT will plot
everything on the same figure instead of creating a new one).

Returns
-------
name : str
A unique name generated by ``tempfile.NamedTemporaryFile``
A unique name generated by :func:`uuid.uuid4`

"""
# Use the tempfile module to generate a unique name.
with NamedTemporaryFile(prefix="gmt-python-") as tmpfile:
return os.path.split(tmpfile.name)[-1]
return uuid.uuid4().hex


class GMTTempFile:
Expand Down
3 changes: 1 addition & 2 deletions pygmt/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@


def test_unique_name():
"Make sure the names start with gmt-python- and are really unique"
"Make sure the names are really unique"
names = [unique_name() for i in range(100)]
assert all([name.startswith("gmt-python-") for name in names])
assert len(names) == len(set(names))


Expand Down