1
1
"""
2
- Alias system converting PyGMT parameters to GMT short-form options.
2
+ PyGMT's alias system for converting PyGMT parameters to GMT short-form options.
3
3
"""
4
4
5
5
import dataclasses
6
6
import inspect
7
7
from collections import defaultdict
8
8
from collections .abc import Mapping , Sequence
9
- from typing import Any
9
+ from typing import Any , Literal
10
10
11
11
from pygmt .helpers .utils import is_nonstr_iter
12
12
13
13
14
14
def value_to_string (
15
15
value : Any ,
16
16
prefix : str = "" , # Default to an empty string to simplify the code logic.
17
- separator : str | None = None ,
17
+ separator : Literal [ "/" , "," ] | None = None ,
18
18
mapping : bool | Mapping = False ,
19
19
) -> str | Sequence [str ] | None :
20
20
"""
@@ -28,14 +28,19 @@ def value_to_string(
28
28
- Any other value will be converted to a string if possible.
29
29
30
30
If a mapping dictionary is provided, the value will be converted to the short-form
31
- value that GMT accepts (e.g., mapping PyGMT long-form argument ``high`` to GMT's
32
- short-form argument ``h ``). For values not in the mapping dictionary, the original
33
- value will be returned. If ``mapping`` is set to ``True``, the first letter of the
34
- long-form argument will be used as the short-form argument.
31
+ string that GMT accepts (e.g., mapping PyGMT long-form argument ``" high" `` to GMT's
32
+ short-form argument ``"h" ``). If the value is not in the mapping dictionary, the
33
+ original value will be returned. If ``mapping`` is set to ``True``, the first letter
34
+ of the long-form argument will be used as the short-form argument.
35
35
36
36
An optional prefix (e.g., `"+o"`) can be added to the beginning of the converted
37
37
string.
38
38
39
+ Need to note that this function doesn't check if the given parameters are valid, to
40
+ avoid the overhead of checking. For example, if ``value`` is a sequence but
41
+ ``separator`` is not specified, a sequence of strings will be returned. ``prefix``
42
+ makes no sense here, but this function won't check it.
43
+
39
44
Parameters
40
45
----------
41
46
value
@@ -92,11 +97,11 @@ def value_to_string(
92
97
if is_nonstr_iter (value ): # Is a sequence.
93
98
value = [str (item ) for item in value ] # Convert to a sequence of strings
94
99
if separator is None :
95
- # A sequence is given but separator is not specified. In this case, return
96
- # a sequence of strings, which is used to support repeated GMT options like
97
- # '-B'. 'prefix' makes no sense here, so ignored.
100
+ # A sequence is given but separator is not specified. Return a sequence of
101
+ # strings, to support repeated GMT options like '-B'. 'prefix' makes no
102
+ # sense and is ignored.
98
103
return value
99
- value = separator .join (value ) # Join the sequence with the separator.
104
+ value = separator .join (value ) # Join the sequence by the separator.
100
105
elif mapping : # Mapping long-form arguments to short-form arguments.
101
106
value = value [0 ] if mapping is True else mapping .get (value , value )
102
107
# Return the final string with the optional prefix.
@@ -143,7 +148,7 @@ def __init__(
143
148
self ,
144
149
name : str ,
145
150
prefix : str = "" ,
146
- separator : str | None = None ,
151
+ separator : Literal [ "/" , "," ] | None = None ,
147
152
mapping : bool | Mapping = False ,
148
153
value : Any = None ,
149
154
):
0 commit comments