diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 6e6e495e244..1bea0e4b886 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1641,12 +1641,13 @@ def virtualfile_out( Examples -------- + >>> from pathlib import Path >>> from pygmt.clib import Session >>> from pygmt.datatypes import _GMT_DATASET >>> from pygmt.helpers import GMTTempFile >>> >>> with GMTTempFile(suffix=".txt") as tmpfile: - ... with open(tmpfile.name, mode="w") as fp: + ... with Path(tmpfile.name).open(mode="w") as fp: ... print("1.0 2.0 3.0 TEXT", file=fp) ... ... # Create a virtual file for storing the output table. @@ -1661,8 +1662,7 @@ def virtualfile_out( ... with lib.virtualfile_out(fname=tmpfile.name) as vouttbl: ... assert vouttbl == tmpfile.name ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") - ... with open(vouttbl, mode="r") as fp: - ... line = fp.readline() + ... line = Path(vouttbl).read_text() ... assert line == "1\t2\t3\tTEXT\n" """ if fname is not None: # Yield the actual file name. @@ -1692,13 +1692,14 @@ def read_virtualfile( Examples -------- + >>> from pathlib import Path >>> from pygmt.clib import Session >>> from pygmt.helpers import GMTTempFile >>> >>> # Read dataset from a virtual file >>> with Session() as lib: ... with GMTTempFile(suffix=".txt") as tmpfile: - ... with open(tmpfile.name, mode="w") as fp: + ... 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") @@ -1779,7 +1780,7 @@ def virtualfile_to_dataset( >>> >>> with GMTTempFile(suffix=".txt") as tmpfile: ... # prepare the sample data file - ... with open(tmpfile.name, mode="w") as fp: + ... with Path(tmpfile.name).open(mode="w") as fp: ... print(">", file=fp) ... print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp) ... print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp) diff --git a/pygmt/datatypes/dataset.py b/pygmt/datatypes/dataset.py index 21953ee9051..a0d0547f3ca 100644 --- a/pygmt/datatypes/dataset.py +++ b/pygmt/datatypes/dataset.py @@ -18,12 +18,13 @@ class _GMT_DATASET(ctp.Structure): # noqa: N801 Examples -------- + >>> from pathlib import Path >>> from pygmt.helpers import GMTTempFile >>> from pygmt.clib import Session >>> >>> with GMTTempFile(suffix=".txt") as tmpfile: ... # Prepare the sample data file - ... with open(tmpfile.name, mode="w") as fp: + ... with Path(tmpfile.name).open(mode="w") as fp: ... print(">", file=fp) ... print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp) ... print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp) @@ -157,12 +158,13 @@ def to_dataframe(self) -> pd.DataFrame: Examples -------- + >>> from pathlib import Path >>> from pygmt.helpers import GMTTempFile >>> from pygmt.clib import Session >>> >>> with GMTTempFile(suffix=".txt") as tmpfile: ... # prepare the sample data file - ... with open(tmpfile.name, mode="w") as fp: + ... with Path(tmpfile.name).open(mode="w") as fp: ... print(">", file=fp) ... print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp) ... print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)