Skip to content

Commit c024489

Browse files
authored
Add sequence_space converter in kwargs_to_string (#325)
* Add sequence_space converter in kwargs_to_string Introduce new 'sequence_space' converter for transforming a Python list or tuple into a space separated string. Useful in cases for e.g. when multiple file inputs can be passed into some module. Also added a doctest example into the kwargs_to_string function. * Simplify check on whether format is in the separators list
1 parent ed73573 commit c024489

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

pygmt/helpers/decorators.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def kwargs_to_strings(convert_bools=True, **conversions):
210210
string
211211
* 'sequence_comma': transforms a sequence into a ``','`` separated string
212212
* 'sequence_plus': transforms a sequence into a ``'+'`` separated string
213+
* 'sequence_space': transforms a sequence into a ``' '`` separated string
213214
214215
Parameters
215216
----------
@@ -224,7 +225,7 @@ def kwargs_to_strings(convert_bools=True, **conversions):
224225
Examples
225226
--------
226227
227-
>>> @kwargs_to_strings(R='sequence', i='sequence_comma')
228+
>>> @kwargs_to_strings(R='sequence', i='sequence_comma', files='sequence_space')
228229
... def module(*args, **kwargs):
229230
... "A module that prints the arguments it received"
230231
... print('{', end='')
@@ -245,21 +246,33 @@ def kwargs_to_strings(convert_bools=True, **conversions):
245246
{}
246247
>>> module(i=[1, 2])
247248
{'i': '1,2'}
249+
>>> module(files=["data1.txt", "data2.txt"])
250+
{'files': 'data1.txt data2.txt'}
248251
>>> # Other non-boolean arguments are passed along as they are
249252
>>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6))
250253
{'bla': (1, 2, 3), 'foo': '', 'i': '5,6'}
251254
args: 123
252255
253256
"""
254-
valid_conversions = ["sequence", "sequence_comma", "sequence_plus"]
257+
valid_conversions = [
258+
"sequence",
259+
"sequence_comma",
260+
"sequence_plus",
261+
"sequence_space",
262+
]
255263

256264
for arg, fmt in conversions.items():
257265
if fmt not in valid_conversions:
258266
raise GMTInvalidInput(
259267
"Invalid conversion type '{}' for argument '{}'.".format(fmt, arg)
260268
)
261269

262-
separators = {"sequence": "/", "sequence_comma": ",", "sequence_plus": "+"}
270+
separators = {
271+
"sequence": "/",
272+
"sequence_comma": ",",
273+
"sequence_plus": "+",
274+
"sequence_space": " ",
275+
}
263276

264277
# Make the actual decorator function
265278
def converter(module_func):
@@ -273,7 +286,7 @@ def new_module(*args, **kwargs):
273286
for arg, fmt in conversions.items():
274287
if arg in kwargs:
275288
value = kwargs[arg]
276-
issequence = fmt in ("sequence", "sequence_comma", "sequence_plus")
289+
issequence = fmt in separators
277290
if issequence and is_nonstr_iter(value):
278291
kwargs[arg] = separators[fmt].join(
279292
"{}".format(item) for item in value

0 commit comments

Comments
 (0)