From 28eb1df81273c018c1be56d768543e80aa8a493e Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 22 Apr 2024 13:14:47 +0800 Subject: [PATCH 1/3] Let validate_output_table_type specify the supported output types --- pygmt/helpers/validators.py | 26 ++++++++++++++++++-------- pygmt/src/triangulate.py | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py index 94916eac1f5..74922261bb4 100644 --- a/pygmt/helpers/validators.py +++ b/pygmt/helpers/validators.py @@ -3,13 +3,16 @@ """ import warnings +from collections.abc import Sequence from typing import Literal from pygmt.exceptions import GMTInvalidInput def validate_output_table_type( - output_type: Literal["pandas", "numpy", "file"], outfile: str | None = None + output_type: Literal["pandas", "numpy", "file"], + valid_types: Sequence[str] = ("pandas", "numpy", "file"), + outfile: str | None = None, ) -> Literal["pandas", "numpy", "file"]: """ Check if the ``output_type`` and ``outfile`` parameters are valid. @@ -17,8 +20,10 @@ def validate_output_table_type( Parameters ---------- output_type - Desired output type of tabular data. Valid values are ``"pandas"``, - ``"numpy"`` and ``"file"``. + Desired output type of tabular data. Valid values are ``"pandas"``, ``"numpy"`` + and ``"file"``. + valid_types + Valid desired output types. outfile File name for saving the result data. Required if ``output_type`` is ``"file"``. If specified, ``output_type`` will be forced to be ``"file"``. @@ -36,23 +41,28 @@ def validate_output_table_type( 'numpy' >>> validate_output_table_type(output_type="file", outfile="output-fname.txt") 'file' + >>> validate_output_table_type(output_type="pandas", valid_types=("pandas", "file")) + 'pandas' >>> validate_output_table_type(output_type="invalid-type") Traceback (most recent call last): ... - pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' either as 'file', ... + pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas' or ... >>> validate_output_table_type("file", outfile=None) Traceback (most recent call last): ... pygmt.exceptions.GMTInvalidInput: Must specify 'outfile' for output_type='file'. + >>> validate_output_table_type(output_type="numpy", valid_types=("pandas", "file")) + Traceback (most recent call last): + ... + pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas' or 'file'. >>> with warnings.catch_warnings(record=True) as w: ... validate_output_table_type("pandas", outfile="not-none.txt") ... assert len(w) == 1 'file' """ - if output_type not in ["file", "numpy", "pandas"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'file', 'numpy', or 'pandas'." - ) + if output_type not in valid_types: + msg = f"Must specify 'output_type' as '{"' or '".join(valid_types)}'." + raise GMTInvalidInput(msg) if output_type == "file" and outfile is None: raise GMTInvalidInput("Must specify 'outfile' for output_type='file'.") if output_type != "file" and outfile is not None: diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index f1b64db38ec..1765bd1d28e 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -233,7 +233,7 @@ def delaunay_triples( ``triangulate`` is a Cartesian or small-geographic area operator and is unaware of periodic or polar boundary conditions. """ - output_type = validate_output_table_type(output_type, outfile) + output_type = validate_output_table_type(output_type, outfile=outfile) with Session() as lib: with ( From 5e926e85ba5ac97b0e33029630d68cbd7b3b02e7 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 22 Apr 2024 15:45:14 +0800 Subject: [PATCH 2/3] Fix --- pygmt/helpers/validators.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py index 74922261bb4..916c7341588 100644 --- a/pygmt/helpers/validators.py +++ b/pygmt/helpers/validators.py @@ -46,7 +46,7 @@ def validate_output_table_type( >>> validate_output_table_type(output_type="invalid-type") Traceback (most recent call last): ... - pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas' or ... + pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas', ... >>> validate_output_table_type("file", outfile=None) Traceback (most recent call last): ... @@ -54,14 +54,18 @@ def validate_output_table_type( >>> validate_output_table_type(output_type="numpy", valid_types=("pandas", "file")) Traceback (most recent call last): ... - pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas' or 'file'. + pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas', or 'file'. >>> with warnings.catch_warnings(record=True) as w: ... validate_output_table_type("pandas", outfile="not-none.txt") ... assert len(w) == 1 'file' """ if output_type not in valid_types: - msg = f"Must specify 'output_type' as '{"' or '".join(valid_types)}'." + msg = ( + "Must specify 'output_type' as " + + ", ".join(f"'{v}'" for v in valid_types[:-1]) + + f", or '{valid_types[-1]}'." + ) raise GMTInvalidInput(msg) if output_type == "file" and outfile is None: raise GMTInvalidInput("Must specify 'outfile' for output_type='file'.") From 3a3df0a6d67edc085575602e4ed160c7d485472d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 6 May 2024 09:48:08 +0800 Subject: [PATCH 3/3] Update docstrings --- pygmt/helpers/validators.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py index 916c7341588..eb040d189ec 100644 --- a/pygmt/helpers/validators.py +++ b/pygmt/helpers/validators.py @@ -20,10 +20,10 @@ def validate_output_table_type( Parameters ---------- output_type - Desired output type of tabular data. Valid values are ``"pandas"``, ``"numpy"`` - and ``"file"``. + Desired output type of tabular data. Default valid values are ``"pandas"``, + ``"numpy"`` and ``"file"``, but can be configured by parameter ``valid_types``. valid_types - Valid desired output types. + Tuple of valid desired output types. outfile File name for saving the result data. Required if ``output_type`` is ``"file"``. If specified, ``output_type`` will be forced to be ``"file"``.