diff --git a/pygmt/helpers/tempfile.py b/pygmt/helpers/tempfile.py index 036de1e6d48..db54681edc9 100644 --- a/pygmt/helpers/tempfile.py +++ b/pygmt/helpers/tempfile.py @@ -2,6 +2,7 @@ Utilities for dealing with temporary file management. """ import os +import uuid from tempfile import NamedTemporaryFile import numpy as np @@ -9,7 +10,7 @@ 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). @@ -17,12 +18,10 @@ def unique_name(): 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: diff --git a/pygmt/tests/test_helpers.py b/pygmt/tests/test_helpers.py index febec2738e4..573f77393c5 100644 --- a/pygmt/tests/test_helpers.py +++ b/pygmt/tests/test_helpers.py @@ -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))