From df571fd02f569f081cd5c669944552e48fb37312 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 19 Dec 2020 02:33:13 -0500 Subject: [PATCH] Improve the readability of the grdview function When I worked on PR #750 based on the implemention of the `grdview` function, I found that the `grdview` codes are a little difficult to understand. The main reason is that `contextlib.ExitStack()` is new to me. Another reason is that these two codes `fname = stack.enter_context(file_context)` and `arg_str = " ".join([fname, build_arg_string(kwargs)])` are separated by the long `if` block. --- pygmt/base_plotting.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pygmt/base_plotting.py b/pygmt/base_plotting.py index 45f0cc6ef35..f0ac7142e9a 100644 --- a/pygmt/base_plotting.py +++ b/pygmt/base_plotting.py @@ -598,18 +598,17 @@ def grdview(self, grid, **kwargs): raise GMTInvalidInput(f"Unrecognized data type for grid: {type(grid)}") with contextlib.ExitStack() as stack: - fname = stack.enter_context(file_context) - if "G" in kwargs: + if "G" in kwargs: # deal with kwargs["G"] if drapegrid is xr.DataArray drapegrid = kwargs["G"] if data_kind(drapegrid) in ("file", "grid"): if data_kind(drapegrid) == "grid": drape_context = lib.virtualfile_from_grid(drapegrid) - drapefile = stack.enter_context(drape_context) - kwargs["G"] = drapefile + kwargs["G"] = stack.enter_context(drape_context) else: raise GMTInvalidInput( f"Unrecognized data type for drapegrid: {type(drapegrid)}" ) + fname = stack.enter_context(file_context) arg_str = " ".join([fname, build_arg_string(kwargs)]) lib.call_module("grdview", arg_str)