1414def _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
163173class 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 ,
0 commit comments