Skip to content

Commit 5d81d2a

Browse files
committed
AliasSystem: Support adding a suffix to a value and simplify Figure.wiggle
1 parent 48c3c92 commit 5d81d2a

2 files changed

Lines changed: 32 additions & 39 deletions

File tree

pygmt/alias.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
def _to_string(
1515
value: Any,
1616
prefix: str = "", # Default to an empty string to simplify the code logic.
17+
suffix: str = "", # Default to an empty string to simplify the code logic.
1718
mapping: Mapping | None = None,
1819
sep: Literal["/", ","] | None = None,
1920
size: int | Sequence[int] | None = None,
@@ -38,20 +39,22 @@ def _to_string(
3839
GMT's short-form argument ``"h"``).
3940
4041
An optional prefix (e.g., `"+o"`) can be added to the beginning of the converted
41-
string.
42+
string, and an optional suffix (e.g., `"+l"`) can be added to the end.
4243
4344
To avoid extra overhead, this function does not validate parameter combinations. For
4445
example, if ``value`` is a sequence but ``sep`` is not specified, the function will
45-
return a sequence of strings. In this case, ``prefix`` has no effect, but the
46-
function does not check for such inconsistencies. The maintainer should ensure that
47-
the parameter combinations are valid.
46+
return a sequence of strings. In this case, ``prefix`` and ``suffix`` have no
47+
effect, but the function does not check for such inconsistencies. The maintainer
48+
should ensure that the parameter combinations are valid.
4849
4950
Parameters
5051
----------
5152
value
5253
The value to convert.
5354
prefix
5455
The string to add as a prefix to the returned value.
56+
suffix
57+
The string to add as a suffix to the returned value.
5558
mapping
5659
A mapping dictionary to map PyGMT's long-form arguments to GMT's short-form.
5760
sep
@@ -90,6 +93,13 @@ def _to_string(
9093
>>> _to_string(False, prefix="+a")
9194
>>> _to_string(None, prefix="+a")
9295
96+
>>> _to_string("blue", suffix="+l")
97+
'blue+l'
98+
>>> _to_string("red", suffix="+r")
99+
'red+r'
100+
>>> _to_string(True, suffix="+l")
101+
'+l'
102+
93103
>>> _to_string("mean", mapping={"mean": "a", "mad": "d", "full": "g"})
94104
'a'
95105
>>> _to_string("invalid", mapping={"mean": "a", "mad": "d", "full": "g"})
@@ -135,9 +145,9 @@ def _to_string(
135145
# None and False are converted to None.
136146
if value is None or value is False:
137147
return None
138-
# True is converted to an empty string with the optional prefix.
148+
# True is converted to an empty string with the optional prefix and suffix.
139149
if value is True:
140-
return f"{prefix}"
150+
return f"{prefix}{suffix}"
141151
# Any non-sequence value is converted to a string.
142152
if not is_nonstr_iter(value):
143153
if mapping:
@@ -148,16 +158,16 @@ def _to_string(
148158
choices=mapping.keys(),
149159
)
150160
value = mapping.get(value, value)
151-
return f"{prefix}{value}"
161+
return f"{prefix}{value}{suffix}"
152162

153163
# Return the sequence if separator is not specified for options like '-B'.
154164
# True in a sequence will be converted to an empty string.
155165
if sep is None:
156166
return [str(item) if item is not True else "" for item in value]
157167
# Join the sequence of values with the separator.
158-
# "prefix" and "mapping" are ignored. We can enable them when needed.
168+
# "prefix", "suffix", and "mapping" are ignored. We can enable them when needed.
159169
_value = sequence_join(value, sep=sep, size=size, ndim=ndim, name=name)
160-
return _value if is_nonstr_iter(_value) else f"{prefix}{_value}"
170+
return _value if is_nonstr_iter(_value) else f"{prefix}{_value}{suffix}"
161171

162172

163173
class Alias:
@@ -172,6 +182,8 @@ class Alias:
172182
The name of the parameter to be used in the error message.
173183
prefix
174184
The string to add as a prefix to the returned value.
185+
suffix
186+
The string to add as a suffix to the returned value.
175187
mapping
176188
A mapping dictionary to map PyGMT's long-form arguments to GMT's short-form.
177189
sep
@@ -189,6 +201,10 @@ class Alias:
189201
>>> par._value
190202
'+o3.0/3.0'
191203
204+
>>> par = Alias("blue", suffix="+l")
205+
>>> par._value
206+
'blue+l'
207+
192208
>>> par = Alias("mean", mapping={"mean": "a", "mad": "d", "full": "g"})
193209
>>> par._value
194210
'a'
@@ -203,17 +219,20 @@ def __init__(
203219
value: Any,
204220
name: str | None = None,
205221
prefix: str = "",
222+
suffix: str = "",
206223
mapping: Mapping | None = None,
207224
sep: Literal["/", ","] | None = None,
208225
size: int | Sequence[int] | None = None,
209226
ndim: int = 1,
210227
):
211228
self.name = name
212229
self.prefix = prefix
230+
self.suffix = suffix
213231
self._value = _to_string(
214232
value=value,
215233
name=name,
216234
prefix=prefix,
235+
suffix=suffix,
217236
mapping=mapping,
218237
sep=sep,
219238
size=size,

pygmt/src/wiggle.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,6 @@
1111
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias
1212

1313

14-
def _parse_fills(fillpositive, fillnegative):
15-
"""
16-
Parse the fillpositive and fillnegative parameters.
17-
18-
>>> _parse_fills("red", "blue")
19-
['red+p', 'blue+n']
20-
>>> _parse_fills(None, "blue")
21-
'blue+n'
22-
>>> _parse_fills("red", None)
23-
'red+p'
24-
>>> _parse_fills(None, None)
25-
"""
26-
_fills = []
27-
if fillpositive is not None:
28-
_fills.append(fillpositive + "+p")
29-
if fillnegative is not None:
30-
_fills.append(fillnegative + "+n")
31-
32-
match len(_fills):
33-
case 0:
34-
return None
35-
case 1:
36-
return _fills[0]
37-
case 2:
38-
return _fills
39-
40-
4114
@fmt_docstring
4215
@use_alias(
4316
D="position",
@@ -138,10 +111,11 @@ def wiggle( # noqa: PLR0913
138111
"""
139112
self._activate_figure()
140113

141-
_fills = _parse_fills(fillpositive, fillnegative)
142-
143114
aliasdict = AliasSystem(
144-
G=Alias(_fills, name="fillpositive/fillnegative"),
115+
G=[
116+
Alias(fillpositive, name="fillpositive", suffix="+p"),
117+
Alias(fillnegative, name="fillnegative", suffix="+n"),
118+
],
145119
).add_common(
146120
B=frame,
147121
J=projection,

0 commit comments

Comments
 (0)