3
3
"""
4
4
5
5
import warnings
6
- from collections .abc import Sequence
7
6
from typing import Literal
8
7
9
8
from pygmt .exceptions import GMTInvalidInput
10
9
11
10
12
11
def validate_output_table_type (
13
- output_type : Literal ["pandas" , "numpy" , "file" ],
14
- valid_types : Sequence [str ] = ("pandas" , "numpy" , "file" ),
15
- outfile : str | None = None ,
12
+ output_type : Literal ["pandas" , "numpy" , "file" ], outfile : str | None = None
16
13
) -> Literal ["pandas" , "numpy" , "file" ]:
17
14
"""
18
15
Check if the ``output_type`` and ``outfile`` parameters are valid.
19
16
20
17
Parameters
21
18
----------
22
19
output_type
23
- Desired output type of tabular data. Default valid values are ``"pandas"``,
24
- ``"numpy"`` and ``"file"``, but can be configured by parameter ``valid_types``.
25
- valid_types
26
- Tuple of valid desired output types.
20
+ Desired output type of tabular data. Valid values are ``"pandas"``,
21
+ ``"numpy"`` and ``"file"``.
27
22
outfile
28
23
File name for saving the result data. Required if ``output_type`` is ``"file"``.
29
24
If specified, ``output_type`` will be forced to be ``"file"``.
@@ -41,32 +36,23 @@ def validate_output_table_type(
41
36
'numpy'
42
37
>>> validate_output_table_type(output_type="file", outfile="output-fname.txt")
43
38
'file'
44
- >>> validate_output_table_type(output_type="pandas", valid_types=("pandas", "file"))
45
- 'pandas'
46
39
>>> validate_output_table_type(output_type="invalid-type")
47
40
Traceback (most recent call last):
48
41
...
49
- pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas ', ...
42
+ pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' either as 'file ', ...
50
43
>>> validate_output_table_type("file", outfile=None)
51
44
Traceback (most recent call last):
52
45
...
53
46
pygmt.exceptions.GMTInvalidInput: Must specify 'outfile' for output_type='file'.
54
- >>> validate_output_table_type(output_type="numpy", valid_types=("pandas", "file"))
55
- Traceback (most recent call last):
56
- ...
57
- pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' as 'pandas', or 'file'.
58
47
>>> with warnings.catch_warnings(record=True) as w:
59
48
... validate_output_table_type("pandas", outfile="not-none.txt")
60
49
... assert len(w) == 1
61
50
'file'
62
51
"""
63
- if output_type not in valid_types :
64
- msg = (
65
- "Must specify 'output_type' as "
66
- + ", " .join (f"'{ v } '" for v in valid_types [:- 1 ])
67
- + f", or '{ valid_types [- 1 ]} '."
52
+ if output_type not in ["file" , "numpy" , "pandas" ]:
53
+ raise GMTInvalidInput (
54
+ "Must specify 'output_type' either as 'file', 'numpy', or 'pandas'."
68
55
)
69
- raise GMTInvalidInput (msg )
70
56
if output_type == "file" and outfile is None :
71
57
raise GMTInvalidInput ("Must specify 'outfile' for output_type='file'." )
72
58
if output_type != "file" and outfile is not None :
0 commit comments