Skip to content

Commit 6c6bb43

Browse files
authored
Figure.savefig: Refactor if-else statements to match-case statements (#3434)
1 parent 781e516 commit 6c6bb43

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

pygmt/figure.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -354,41 +354,38 @@ def savefig( # noqa: PLR0912
354354
prefix, suffix = fname.with_suffix("").as_posix(), fname.suffix
355355
ext = suffix[1:].lower() # Remove the . and normalize to lowercase
356356

357-
if ext == "jpeg": # Alias jpeg to jpg
358-
ext = "jpg"
359-
elif ext == "tiff": # GeoTIFF
360-
kwargs["W"] = "+g"
361-
elif ext == "kml": # KML
362-
kwargs["W"] = "+k"
363-
364-
if ext not in fmts:
365-
if ext == "ps":
366-
raise GMTInvalidInput(
367-
"Extension '.ps' is not supported. "
368-
"Please use '.eps' or '.pdf' instead."
369-
)
370-
raise GMTInvalidInput(f"Unknown extension '.{ext}'.")
357+
match ext:
358+
case "jpeg": # Alias jpeg to jpg
359+
ext = "jpg"
360+
case "tiff": # GeoTIFF
361+
kwargs["W"] = "+g"
362+
case "kml": # KML
363+
kwargs["W"] = "+k"
364+
case "ps":
365+
msg = "Extension '.ps' is not supported. Use '.eps' or '.pdf' instead."
366+
raise GMTInvalidInput(msg)
367+
case ext if ext not in fmts:
368+
raise GMTInvalidInput(f"Unknown extension '.{ext}'.")
369+
371370
fmt = fmts[ext]
372371
if transparent:
373372
if fmt != "g":
374-
raise GMTInvalidInput(
375-
f"Transparency unavailable for '{ext}', only for png."
376-
)
373+
msg = f"Transparency unavailable for '{ext}', only for png."
374+
raise GMTInvalidInput(msg)
377375
fmt = fmt.upper()
378376
if anti_alias:
379377
kwargs["Qt"] = 2
380378
kwargs["Qg"] = 2
381379

382380
if worldfile:
383381
if ext in {"eps", "kml", "pdf", "tiff"}:
384-
raise GMTInvalidInput(
385-
f"Saving a world file is not supported for '{ext}' format."
386-
)
382+
msg = f"Saving a world file is not supported for '{ext}' format."
383+
raise GMTInvalidInput(msg)
387384
kwargs["W"] = True
388385

389386
self.psconvert(prefix=prefix, fmt=fmt, crop=crop, **kwargs)
390387

391-
# Remove the .pgw world file if exists
388+
# Remove the .pgw world file if exists.
392389
# Not necessary after GMT 6.5.0.
393390
# See upstream fix https://github.com/GenericMappingTools/gmt/pull/7865
394391
if ext == "tiff":

0 commit comments

Comments
 (0)