Skip to content

Commit c1a074b

Browse files
authored
Refactor text to use virtualfile_from_vectors instead of pandas tempfile (#559)
Non user-facing refactor to avoid the use of temporary files in the implementation of `text`.
1 parent db42684 commit c1a074b

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

pygmt/base_plotting.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Does not define any special non-GMT methods (savefig, show, etc).
44
"""
55
import contextlib
6-
import csv
76
import numpy as np
87
import pandas as pd
98

@@ -14,7 +13,6 @@
1413
dummy_context,
1514
data_kind,
1615
fmt_docstring,
17-
GMTTempFile,
1816
use_alias,
1917
kwargs_to_strings,
2018
)
@@ -986,28 +984,16 @@ def text(
986984
if position is not None and isinstance(position, str):
987985
kwargs["F"] += f'+c{position}+t"{text}"'
988986

989-
with GMTTempFile(suffix=".txt") as tmpfile:
990-
with Session() as lib:
991-
fname = textfiles if kind == "file" else ""
992-
if kind == "vectors":
993-
if position is not None:
994-
fname = ""
995-
else:
996-
pd.DataFrame.from_dict(
997-
{
998-
"x": np.atleast_1d(x),
999-
"y": np.atleast_1d(y),
1000-
"text": np.atleast_1d(text),
1001-
}
1002-
).to_csv(
1003-
tmpfile.name,
1004-
sep="\t",
1005-
header=False,
1006-
index=False,
1007-
quoting=csv.QUOTE_NONE,
1008-
)
1009-
fname = tmpfile.name
1010-
987+
with Session() as lib:
988+
file_context = dummy_context(textfiles) if kind == "file" else ""
989+
if kind == "vectors":
990+
if position is not None:
991+
file_context = dummy_context("")
992+
else:
993+
file_context = lib.virtualfile_from_vectors(
994+
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
995+
)
996+
with file_context as fname:
1011997
arg_str = " ".join([fname, build_arg_string(kwargs)])
1012998
lib.call_module("text", arg_str)
1013999

0 commit comments

Comments
 (0)