Skip to content

Commit 86278cb

Browse files
committed
Refactor if-else using match statements
1 parent ce926a0 commit 86278cb

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

pygmt/src/x2sys_cross.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -197,26 +197,25 @@ def x2sys_cross(
197197

198198
file_contexts = []
199199
for track in tracks:
200-
kind = data_kind(track)
201-
if kind == "file":
202-
file_contexts.append(contextlib.nullcontext(track))
203-
elif kind == "matrix":
204-
# find suffix (-E) of trackfiles used (e.g. xyz, csv, etc) from
205-
# $X2SYS_HOME/TAGNAME/TAGNAME.tag file
206-
lastline = (
207-
Path(os.environ["X2SYS_HOME"], kwargs["T"], f"{kwargs['T']}.tag")
208-
.read_text(encoding="utf8")
209-
.strip()
210-
.split("\n")[-1]
211-
) # e.g. "-Dxyz -Etsv -I1/1"
212-
for item in sorted(lastline.split()): # sort list alphabetically
213-
if item.startswith(("-E", "-D")): # prefer -Etsv over -Dxyz
214-
suffix = item[2:] # e.g. tsv (1st choice) or xyz (2nd choice)
215-
216-
# Save pandas.DataFrame track data to temporary file
217-
file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix))
218-
else:
219-
raise GMTInvalidInput(f"Unrecognized data type: {type(track)}")
200+
match data_kind(track):
201+
case "file":
202+
file_contexts.append(contextlib.nullcontext(track))
203+
case "matrix":
204+
# find suffix (-E) of trackfiles used (e.g. xyz, csv, etc) from
205+
# $X2SYS_HOME/TAGNAME/TAGNAME.tag file
206+
tagfile = Path(
207+
os.environ["X2SYS_HOME"], kwargs["T"], f"{kwargs['T']}.tag"
208+
)
209+
# Last line is like "-Dxyz -Etsv -I1/1"
210+
lastline = tagfile.read_text().splitlines()[-1]
211+
for item in sorted(lastline.split()): # sort list alphabetically
212+
if item.startswith(("-E", "-D")): # prefer -Etsv over -Dxyz
213+
suffix = item[2:] # e.g. tsv (1st choice) or xyz (2nd choice)
214+
215+
# Save pandas.DataFrame track data to temporary file
216+
file_contexts.append(tempfile_from_dftrack(track=track, suffix=suffix))
217+
case _:
218+
raise GMTInvalidInput(f"Unrecognized data type: {type(track)}")
220219

221220
with Session() as lib:
222221
with lib.virtualfile_out(kind="dataset", fname=outfile) as vouttbl:

0 commit comments

Comments
 (0)