Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor text to use virtualfile_from_vectors instead of pandas tempfile #559

Merged
merged 4 commits into from
Sep 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)
Comment on lines +987 to +995
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more readable to rewrite the codes to:

if kind == "file":
   ...
elif kind == "vectors":
  ...
else:
  ...

Copy link
Member Author

@weiji14 weiji14 Sep 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would be more readable. I think the only reason I wrote it that way was because pylint complained about too many if-statements:

R0912: Too many branches (13/12) (too-many-branches)

If readability is more important, I can increase the branch limit on our .pylintrc file, or I can try refactoring more of the code in text to reduce the number of if-statements. OR, we can just merge this in now and worry about it in the next PR that will fix #483.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OR, we can just merge this in now and worry about it in the next PR that will fix #483.

OK to me.

with file_context as fname:
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("text", arg_str)

Expand Down