Skip to content

Commit

Permalink
Refactor doctests to pass list of arguments to the Session.call_modul…
Browse files Browse the repository at this point in the history
…e method (#3255)
  • Loading branch information
seisman authored May 17, 2024
1 parent 744aeda commit 8839e7b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
48 changes: 24 additions & 24 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
)
from pygmt.helpers import (
data_kind,
fmt_docstring,
tempfile_from_geojson,
tempfile_from_image,
)
Expand Down Expand Up @@ -146,7 +145,7 @@ class Session:
... with GMTTempFile() as fout:
... # Call the grdinfo module with the virtual file as input
... # and the temp file as output.
... ses.call_module("grdinfo", f"{fin} -C ->{fout.name}")
... ses.call_module("grdinfo", [fin, "-C", f"->{fout.name}"])
... # Read the contents of the temp file before it's deleted.
... print(fout.read().strip())
-55 -47 -24 -10 190 981 1 1 8 14 1 1
Expand Down Expand Up @@ -543,18 +542,20 @@ def get_common(self, option):
Examples
--------
>>> with Session() as lib:
... lib.call_module("basemap", "-R0/10/10/15 -JX5i/2.5i -Baf -Ve")
... lib.call_module(
... "basemap", ["-R0/10/10/15", "-JX5i/2.5i", "-Baf", "-Ve"]
... )
... region = lib.get_common("R")
... projection = lib.get_common("J")
... timestamp = lib.get_common("U")
... verbose = lib.get_common("V")
... lib.call_module("plot", "-T -Xw+1i -Yh-1i")
... lib.call_module("plot", ["-T", "-Xw+1i", "-Yh-1i"])
... xshift = lib.get_common("X") # xshift/yshift are in inches
... yshift = lib.get_common("Y")
>>> print(region, projection, timestamp, verbose, xshift, yshift)
[ 0. 10. 10. 15.] True False 3 6.0 1.5
>>> with Session() as lib:
... lib.call_module("basemap", "-R0/10/10/15 -JX5i/2.5i -Baf")
... lib.call_module("basemap", ["-R0/10/10/15", "-JX5i/2.5i", "-Baf"])
... lib.get_common("A")
Traceback (most recent call last):
...
Expand Down Expand Up @@ -1180,8 +1181,7 @@ def open_virtualfile(self, family, geometry, direction, data):
... with lib.open_virtualfile(*vfargs) as vfile:
... # Send the output to a temp file so that we can read it
... with GMTTempFile() as ofile:
... args = f"{vfile} ->{ofile.name}"
... lib.call_module("info", args)
... lib.call_module("info", [vfile, f"->{ofile.name}"])
... print(ofile.read().strip())
<vector memory>: N = 5 <0/4> <5/9>
"""
Expand Down Expand Up @@ -1288,7 +1288,7 @@ def virtualfile_from_vectors(self, *vectors):
... with ses.virtualfile_from_vectors(x, y, z) as fin:
... # Send the output to a file so that we can read it
... with GMTTempFile() as fout:
... ses.call_module("info", f"{fin} ->{fout.name}")
... ses.call_module("info", [fin, f"->{fout.name}"])
... print(fout.read().strip())
<vector memory>: N = 3 <1/3> <4/6> <7/9>
"""
Expand Down Expand Up @@ -1398,7 +1398,7 @@ def virtualfile_from_matrix(self, matrix):
... with ses.virtualfile_from_matrix(data) as fin:
... # Send the output to a file so that we can read it
... with GMTTempFile() as fout:
... ses.call_module("info", f"{fin} ->{fout.name}")
... ses.call_module("info", [fin, f"->{fout.name}"])
... print(fout.read().strip())
<matrix memory>: N = 4 <0/9> <1/10> <2/11>
"""
Expand Down Expand Up @@ -1478,8 +1478,9 @@ def virtualfile_from_grid(self, grid):
... with ses.virtualfile_from_grid(data) as fin:
... # Send the output to a file so that we can read it
... with GMTTempFile() as fout:
... args = f"{fin} -L0 -Cn ->{fout.name}"
... ses.call_module("grdinfo", args)
... ses.call_module(
... "grdinfo", [fin, "-L0", "-Cn", f"->{fout.name}"]
... )
... print(fout.read().strip())
-55 -47 -24 -10 190 981 1 1 8 14 1 1
>>> # The output is: w e s n z0 z1 dx dy n_columns n_rows reg gtype
Expand Down Expand Up @@ -1510,7 +1511,6 @@ def virtualfile_from_grid(self, grid):
with self.open_virtualfile(*args) as vfile:
yield vfile

@fmt_docstring
def virtualfile_in( # noqa: PLR0912
self,
check_kind=None,
Expand Down Expand Up @@ -1571,7 +1571,7 @@ def virtualfile_in( # noqa: PLR0912
... with ses.virtualfile_in(check_kind="vector", data=data) as fin:
... # Send the output to a file so that we can read it
... with GMTTempFile() as fout:
... ses.call_module("info", fin + " ->" + fout.name)
... ses.call_module("info", [fin, f"->{fout.name}"])
... print(fout.read().strip())
<vector memory>: N = 3 <7/9> <4/6> <1/3>
"""
Expand Down Expand Up @@ -1718,15 +1718,15 @@ def virtualfile_out(
... # Create a virtual file for storing the output table.
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... ds = lib.read_virtualfile(vouttbl, kind="dataset")
... assert isinstance(ds.contents, _GMT_DATASET)
...
... # Write data to an actual file without creating a virtual file.
... with Session() as lib:
... with lib.virtualfile_out(fname=tmpfile.name) as vouttbl:
... assert vouttbl == tmpfile.name
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... line = Path(vouttbl).read_text()
... assert line == "1\t2\t3\tTEXT\n"
"""
Expand Down Expand Up @@ -1798,7 +1798,7 @@ def read_virtualfile(
... with Path(tmpfile.name).open(mode="w") as fp:
... print("1.0 2.0 3.0 TEXT", file=fp)
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... # Read the virtual file as a void pointer
... void_pointer = lib.read_virtualfile(vouttbl)
... assert isinstance(void_pointer, int) # void pointer is an int
Expand All @@ -1809,7 +1809,7 @@ def read_virtualfile(
>>> # Read grid from a virtual file
>>> with Session() as lib:
... with lib.virtualfile_out(kind="grid") as voutgrd:
... lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg")
... lib.call_module("read", ["@earth_relief_01d_g", voutgrd, "-Tg"])
... # Read the virtual file as a void pointer
... void_pointer = lib.read_virtualfile(voutgrd)
... assert isinstance(void_pointer, int) # void pointer is an int
Expand Down Expand Up @@ -1905,7 +1905,7 @@ def virtualfile_to_dataset(
... with lib.virtualfile_out(
... kind="dataset", fname=outtmp.name
... ) as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... result = lib.virtualfile_to_dataset(
... vfname=vouttbl, output_type="file"
... )
Expand All @@ -1915,7 +1915,7 @@ def virtualfile_to_dataset(
... # strings output
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... outstr = lib.virtualfile_to_dataset(
... vfname=vouttbl, output_type="strings"
... )
Expand All @@ -1925,7 +1925,7 @@ def virtualfile_to_dataset(
... # numpy output
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... outnp = lib.virtualfile_to_dataset(
... vfname=vouttbl, output_type="numpy"
... )
Expand All @@ -1934,7 +1934,7 @@ def virtualfile_to_dataset(
... # pandas output
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... outpd = lib.virtualfile_to_dataset(
... vfname=vouttbl, output_type="pandas"
... )
Expand All @@ -1943,7 +1943,7 @@ def virtualfile_to_dataset(
... # pandas output with specified column names
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... outpd2 = lib.virtualfile_to_dataset(
... vfname=vouttbl,
... output_type="pandas",
Expand Down Expand Up @@ -2026,7 +2026,7 @@ def virtualfile_to_raster(
... with GMTTempFile(suffix=".nc") as tmpfile:
... outgrid = tmpfile.name
... with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
... lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg")
... lib.call_module("read", ["@earth_relief_01d_g", voutgrd, "-Tg"])
... result = lib.virtualfile_to_raster(
... vfname=voutgrd, outgrid=outgrid
... )
Expand All @@ -2036,7 +2036,7 @@ def virtualfile_to_raster(
... # xarray.DataArray output
... outgrid = None
... with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
... lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg")
... lib.call_module("read", ["@earth_relief_01d_g", voutgrd, "-Tg"])
... result = lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
... assert isinstance(result, xr.DataArray)
"""
Expand Down
4 changes: 2 additions & 2 deletions pygmt/datatypes/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class _GMT_DATASET(ctp.Structure): # noqa: N801
... # Read the data file
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... # The dataset
... ds = lib.read_virtualfile(vouttbl, kind="dataset").contents
... print(ds.n_tables, ds.n_columns, ds.n_segments)
Expand Down Expand Up @@ -224,7 +224,7 @@ def to_dataframe(
... print("10.0 11.0 12.0 TEXT123 TEXT456789", file=fp)
... with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vouttbl:
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
... lib.call_module("read", [tmpfile.name, vouttbl, "-Td"])
... ds = lib.read_virtualfile(vouttbl, kind="dataset")
... text = ds.contents.to_strings()
... df = ds.contents.to_dataframe(header=0)
Expand Down
4 changes: 2 additions & 2 deletions pygmt/datatypes/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _GMT_GRID(ctp.Structure): # noqa: N801
>>> from pygmt.clib import Session
>>> with Session() as lib:
... with lib.virtualfile_out(kind="grid") as voutgrd:
... lib.call_module("read", f"@static_earth_relief.nc {voutgrd} -Tg")
... lib.call_module("read", ["@static_earth_relief.nc", voutgrd, "-Tg"])
... # Read the grid from the virtual file
... grid = lib.read_virtualfile(voutgrd, kind="grid").contents
... # The grid header
Expand Down Expand Up @@ -106,7 +106,7 @@ def to_dataarray(self) -> xr.DataArray:
>>> from pygmt.clib import Session
>>> with Session() as lib:
... with lib.virtualfile_out(kind="grid") as voutgrd:
... lib.call_module("read", f"@static_earth_relief.nc {voutgrd} -Tg")
... lib.call_module("read", ["@static_earth_relief.nc", voutgrd, "-Tg"])
... # Read the grid from the virtual file
... grid = lib.read_virtualfile(voutgrd, kind="grid")
... # Convert to xarray.DataArray and use it later
Expand Down

0 comments on commit 8839e7b

Please sign in to comment.