From 3841bb6ff98b584a7dbcdbd94db296c419388fc8 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Thu, 7 Apr 2022 08:18:21 +0800 Subject: [PATCH] Allow passing None explicitly to pygmt functions Part 3 (#1872) --- pygmt/figure.py | 4 ++-- pygmt/src/grdclip.py | 5 ++--- pygmt/src/grdcut.py | 5 ++--- pygmt/src/grdfill.py | 7 +++---- pygmt/src/grdfilter.py | 5 ++--- pygmt/src/grdlandmask.py | 7 +++---- pygmt/src/grdproject.py | 7 +++---- pygmt/src/grdsample.py | 5 ++--- pygmt/src/info.py | 2 +- pygmt/src/legend.py | 5 ++--- pygmt/src/sph2grd.py | 5 ++--- pygmt/src/sphdistance.py | 2 +- pygmt/src/xyz2grd.py | 2 +- 13 files changed, 26 insertions(+), 35 deletions(-) diff --git a/pygmt/figure.py b/pygmt/figure.py index b922705d752..0597c6dd016 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -222,7 +222,7 @@ def psconvert(self, icc_gray=False, **kwargs): """ kwargs = self._preprocess(**kwargs) # Default cropping the figure to True - if "A" not in kwargs: + if kwargs.get("A") is None: kwargs["A"] = "" if icc_gray: @@ -231,7 +231,7 @@ def psconvert(self, icc_gray=False, **kwargs): " and will be removed in v0.8.0." ) warnings.warn(msg, category=FutureWarning, stacklevel=2) - if "N" not in kwargs: + if kwargs.get("N") is None: kwargs["N"] = "+i" else: kwargs["N"] += "+i" diff --git a/pygmt/src/grdclip.py b/pygmt/src/grdclip.py index f7c4d951d31..5aee7b64dca 100644 --- a/pygmt/src/grdclip.py +++ b/pygmt/src/grdclip.py @@ -104,9 +104,8 @@ def grdclip(grid, **kwargs): with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdclip", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/grdcut.py b/pygmt/src/grdcut.py index 5c4f34efe19..7e067b77549 100644 --- a/pygmt/src/grdcut.py +++ b/pygmt/src/grdcut.py @@ -106,9 +106,8 @@ def grdcut(grid, **kwargs): with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdcut", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/grdfill.py b/pygmt/src/grdfill.py index aa7623b9d5b..7c72c11ce9e 100644 --- a/pygmt/src/grdfill.py +++ b/pygmt/src/grdfill.py @@ -63,15 +63,14 @@ def grdfill(grid, **kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ - if "A" not in kwargs and "L" not in kwargs: + if kwargs.get("A") is None and kwargs.get("L") is None: raise GMTInvalidInput("At least parameter 'mode' or 'L' must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdfill", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/grdfilter.py b/pygmt/src/grdfilter.py index 6492f42fbbc..f7042c05b0b 100644 --- a/pygmt/src/grdfilter.py +++ b/pygmt/src/grdfilter.py @@ -145,9 +145,8 @@ def grdfilter(grid, **kwargs): with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdfilter", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/grdlandmask.py b/pygmt/src/grdlandmask.py index e6a39657bc2..cba664b3db4 100644 --- a/pygmt/src/grdlandmask.py +++ b/pygmt/src/grdlandmask.py @@ -101,14 +101,13 @@ def grdlandmask(**kwargs): >>> # and a y-range of 30 to 35 >>> landmask = pygmt.grdlandmask(spacing=1, region=[125, 130, 30, 35]) """ - if "I" not in kwargs or "R" not in kwargs: + if kwargs.get("I") is None or kwargs.get("R") is None: raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdlandmask", build_arg_string(kwargs)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/grdproject.py b/pygmt/src/grdproject.py index 4ad9fc2c7c0..abf33dc7df1 100644 --- a/pygmt/src/grdproject.py +++ b/pygmt/src/grdproject.py @@ -112,15 +112,14 @@ def grdproject(grid, **kwargs): >>> # to "rectangular" >>> new_grid = pygmt.grdproject(grid=grid, projection="M10c", inverse=True) """ - if "J" not in kwargs: + if kwargs.get("J") is None: raise GMTInvalidInput("The projection must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdproject", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/grdsample.py b/pygmt/src/grdsample.py index 4e84e0827ee..bd4ed227663 100644 --- a/pygmt/src/grdsample.py +++ b/pygmt/src/grdsample.py @@ -96,9 +96,8 @@ def grdsample(grid, **kwargs): with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("grdsample", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/info.py b/pygmt/src/info.py index 00f8f7ed66e..fa947a7cc63 100644 --- a/pygmt/src/info.py +++ b/pygmt/src/info.py @@ -90,7 +90,7 @@ def info(data, **kwargs): ) result = tmpfile.read() - if any(arg in kwargs for arg in ["C", "I", "T"]): + if any(kwargs.get(arg) is not None for arg in ["C", "I", "T"]): # Converts certain output types into a numpy array # instead of a raw string that is less useful. if result.startswith(("-R", "-T")): # e.g. -R0/1/2/3 or -T0/9/1 diff --git a/pygmt/src/legend.py b/pygmt/src/legend.py index 6379b65e417..e77b8a9fd4a 100644 --- a/pygmt/src/legend.py +++ b/pygmt/src/legend.py @@ -74,10 +74,9 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg """ kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access - if "D" not in kwargs: + if kwargs.get("D") is None: kwargs["D"] = position - - if "F" not in kwargs: + if kwargs.get("F") is None: kwargs["F"] = box with Session() as lib: diff --git a/pygmt/src/sph2grd.py b/pygmt/src/sph2grd.py index e16a368ee9a..c774eb15483 100644 --- a/pygmt/src/sph2grd.py +++ b/pygmt/src/sph2grd.py @@ -79,9 +79,8 @@ def sph2grd(data, **kwargs): with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="vector", data=data) with file_context as infile: - if "G" not in kwargs: # if outgrid is unset, output to tempfile - kwargs.update({"G": tmpfile.name}) - outgrid = kwargs["G"] + if (outgrid := kwargs.get("G")) is None: + kwargs["G"] = outgrid = tmpfile.name # output to tmpfile lib.call_module("sph2grd", build_arg_string(kwargs, infile=infile)) return load_dataarray(outgrid) if outgrid == tmpfile.name else None diff --git a/pygmt/src/sphdistance.py b/pygmt/src/sphdistance.py index 3d083b0a6a4..4682ead0e1f 100644 --- a/pygmt/src/sphdistance.py +++ b/pygmt/src/sphdistance.py @@ -100,7 +100,7 @@ def sphdistance(data=None, x=None, y=None, **kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ - if "I" not in kwargs or "R" not in kwargs: + if kwargs.get("I") is None or kwargs.get("R") is None: raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index cf31e3a8be7..61ab99bb28e 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -148,7 +148,7 @@ def xyz2grd(data=None, x=None, y=None, z=None, **kwargs): ... x=xx, y=yy, z=zz, spacing=(1.0, 0.5), region=[0, 3, 10, 13] ... ) """ - if "I" not in kwargs or "R" not in kwargs: + if kwargs.get("I") is None or kwargs.get("R") is None: raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: