From c1a074b805c089788b967f8ef2b4da9cbf21d7ea Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 7 Sep 2020 11:29:02 +1200 Subject: [PATCH] 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`. --- pygmt/base_plotting.py | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index 811516c1335..a3f682bebf9 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -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 @@ -14,7 +13,6 @@ dummy_context, data_kind, fmt_docstring, - GMTTempFile, use_alias, kwargs_to_strings, ) @@ -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)