Skip to content

Commit

Permalink
Refactor text to use virtualfile_from_vectors instead of pandas tempf…
Browse files Browse the repository at this point in the history
…ile (#559)

Non user-facing refactor to avoid the use of
temporary files in the implementation of `text`.
  • Loading branch information
weiji14 authored Sep 6, 2020
1 parent db42684 commit c1a074b
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions pygmt/base_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Does not define any special non-GMT methods (savefig, show, etc).
"""
import contextlib
import csv
import numpy as np
import pandas as pd

Expand All @@ -14,7 +13,6 @@
dummy_context,
data_kind,
fmt_docstring,
GMTTempFile,
use_alias,
kwargs_to_strings,
)
Expand Down Expand Up @@ -986,28 +984,16 @@ def text(
if position is not None and isinstance(position, str):
kwargs["F"] += f'+c{position}+t"{text}"'

with GMTTempFile(suffix=".txt") as tmpfile:
with Session() as lib:
fname = textfiles if kind == "file" else ""
if kind == "vectors":
if position is not None:
fname = ""
else:
pd.DataFrame.from_dict(
{
"x": np.atleast_1d(x),
"y": np.atleast_1d(y),
"text": np.atleast_1d(text),
}
).to_csv(
tmpfile.name,
sep="\t",
header=False,
index=False,
quoting=csv.QUOTE_NONE,
)
fname = tmpfile.name

with Session() as lib:
file_context = dummy_context(textfiles) if kind == "file" else ""
if kind == "vectors":
if position is not None:
file_context = dummy_context("")
else:
file_context = lib.virtualfile_from_vectors(
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
)
with file_context as fname:
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("text", arg_str)

Expand Down

0 comments on commit c1a074b

Please sign in to comment.