1
1
"""
2
- Alias system that converts PyGMT parameters to GMT short-form options.
2
+ Alias system converting PyGMT parameters to GMT short-form options.
3
3
"""
4
4
5
5
import dataclasses
@@ -20,14 +20,18 @@ def value_to_string(
20
20
"""
21
21
Convert any value to a string, a sequence of strings or None.
22
22
23
- ``None`` or ``False`` will be converted to ``None``.
23
+ - ``None`` or ``False`` will be converted to ``None``.
24
+ - ``True`` will be converted to an empty string.
25
+ - A sequence will be joined by the separator if a separator is provided. Otherwise,
26
+ each item in the sequence will be converted to a string and a sequence of strings
27
+ will be returned.
28
+ - Any other value will be converted to a string if possible.
24
29
25
- ``True`` will be converted to an empty string. If the value is a sequence and a
26
- separator is provided, the sequence will be joined by the separator. Otherwise, each
27
- item in the sequence will be converted to a string and a sequence of strings will be
28
- returned. Any other value will be converted to a string if possible. It also tried
29
- to convert PyGMT's long-form arguments into GMT's short-form arguments by using a
30
- mapping dictionary or simply using the first letter of the long-form arguments.
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
35
32
36
An optional prefix (e.g., `"+o"`) can be added to the beginning of the converted
33
37
string.
@@ -37,12 +41,17 @@ def value_to_string(
37
41
value
38
42
The value to convert.
39
43
prefix
40
- The string to add as a prefix to the value.
44
+ The string to add as a prefix to the returned value.
41
45
separator
42
46
The separator to use if the value is a sequence.
43
47
mapping
44
- Map long-form arguments to GMT's short-form arguments. If ``True``, will use the
45
- first letter of the long-form arguments.
48
+ A mapping dictionary or ``True`` to map long-form arguments to GMT's short-form
49
+ arguments. If ``True``, will use the first letter of the long-form arguments.
50
+
51
+ Returns
52
+ -------
53
+ ret
54
+ The converted value.
46
55
47
56
Examples
48
57
--------
@@ -66,8 +75,6 @@ def value_to_string(
66
75
['xaf', 'yaf', 'WSen']
67
76
>>> value_to_string("high", mapping=True)
68
77
'h'
69
- >>> value_to_string("low", mapping=True)
70
- 'l'
71
78
>>> value_to_string("mean", mapping={"mean": "a", "mad": "d", "full": "g"})
72
79
'a'
73
80
>>> value_to_string("invalid", mapping={"mean": "a", "mad": "d", "full": "g"})
@@ -77,20 +84,20 @@ def value_to_string(
77
84
if value is None or value is False :
78
85
return None
79
86
# True means the parameter is specified, returns an empty string with the optional
80
- # prefix (' prefix' defaults to an empty string!).
87
+ # prefix. We don't have to check ' prefix' since it defaults to an empty string!
81
88
if value is True :
82
89
return f"{ prefix } "
83
90
84
- # Convert any value to a string or a sequence of strings
91
+ # Convert any value to a string or a sequence of strings.
85
92
if is_nonstr_iter (value ): # Is a sequence
86
93
value = [str (item ) for item in value ] # Convert to a sequence of strings
87
94
if separator is None :
88
95
# A sequence is given but separator is not specified. In this case, return
89
96
# a sequence of strings, which is used to support repeated GMT options like
90
97
# '-B'. 'prefix' makes no sense here, so ignored.
91
98
return value
92
- value = separator .join (value ) # Join the sequence by the specified separator.
93
- if mapping : # Mapping long-form arguments to short-form arguments
99
+ value = separator .join (value ) # Join the sequence with the separator.
100
+ elif mapping : # Mapping long-form arguments to short-form arguments.
94
101
value = value [0 ] if mapping is True else mapping .get (value , value )
95
102
return f"{ prefix } { value } "
96
103
0 commit comments