Skip to content

Commit 63e8959

Browse files
seismanleouieda
authored andcommitted
Use uuid.uuid4 to generate unique names (#274)
Use `uuid.uuid4().hex` to generate unique names. Fixes #273
1 parent 7e18838 commit 63e8959

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

pygmt/helpers/tempfile.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
22
Utilities for dealing with temporary file management.
33
"""
44
import os
5+
import uuid
56
from tempfile import NamedTemporaryFile
67

78
import numpy as np
89

910

1011
def unique_name():
1112
"""
12-
Generate a unique name with the prefix 'gmt-python-'.
13+
Generate a unique name.
1314
1415
Useful for generating unique names for figures (otherwise GMT will plot
1516
everything on the same figure instead of creating a new one).
1617
1718
Returns
1819
-------
1920
name : str
20-
A unique name generated by ``tempfile.NamedTemporaryFile``
21+
A unique name generated by :func:`uuid.uuid4`
2122
2223
"""
23-
# Use the tempfile module to generate a unique name.
24-
with NamedTemporaryFile(prefix="gmt-python-") as tmpfile:
25-
return os.path.split(tmpfile.name)[-1]
24+
return uuid.uuid4().hex
2625

2726

2827
class GMTTempFile:

pygmt/tests/test_helpers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111

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

1817

0 commit comments

Comments
 (0)