From 0e501f363495106e5474a5f33150da9cdcd9fac3 Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Sat, 7 Sep 2019 12:01:23 +1200 Subject: [PATCH 1/2] 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. --- pygmt/helpers/decorators.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pygmt/helpers/decorators.py b/pygmt/helpers/decorators.py index 4b68a90f14e..47bd4377362 100644 --- a/pygmt/helpers/decorators.py +++ b/pygmt/helpers/decorators.py @@ -210,6 +210,7 @@ def kwargs_to_strings(convert_bools=True, **conversions): string * 'sequence_comma': transforms a sequence into a ``','`` separated string * 'sequence_plus': transforms a sequence into a ``'+'`` separated string + * 'sequence_space': transforms a sequence into a ``' '`` separated string Parameters ---------- @@ -224,7 +225,7 @@ def kwargs_to_strings(convert_bools=True, **conversions): Examples -------- - >>> @kwargs_to_strings(R='sequence', i='sequence_comma') + >>> @kwargs_to_strings(R='sequence', i='sequence_comma', files='sequence_space') ... def module(*args, **kwargs): ... "A module that prints the arguments it received" ... print('{', end='') @@ -245,13 +246,20 @@ def kwargs_to_strings(convert_bools=True, **conversions): {} >>> module(i=[1, 2]) {'i': '1,2'} + >>> module(files=["data1.txt", "data2.txt"]) + {'files': 'data1.txt data2.txt'} >>> # Other non-boolean arguments are passed along as they are >>> module(123, bla=(1, 2, 3), foo=True, A=False, i=(5, 6)) {'bla': (1, 2, 3), 'foo': '', 'i': '5,6'} args: 123 """ - valid_conversions = ["sequence", "sequence_comma", "sequence_plus"] + valid_conversions = [ + "sequence", + "sequence_comma", + "sequence_plus", + "sequence_space", + ] for arg, fmt in conversions.items(): if fmt not in valid_conversions: @@ -259,7 +267,12 @@ def kwargs_to_strings(convert_bools=True, **conversions): "Invalid conversion type '{}' for argument '{}'.".format(fmt, arg) ) - separators = {"sequence": "/", "sequence_comma": ",", "sequence_plus": "+"} + separators = { + "sequence": "/", + "sequence_comma": ",", + "sequence_plus": "+", + "sequence_space": " ", + } # Make the actual decorator function def converter(module_func): @@ -273,7 +286,12 @@ def new_module(*args, **kwargs): for arg, fmt in conversions.items(): if arg in kwargs: value = kwargs[arg] - issequence = fmt in ("sequence", "sequence_comma", "sequence_plus") + issequence = fmt in ( + "sequence", + "sequence_comma", + "sequence_plus", + "sequence_space", + ) if issequence and is_nonstr_iter(value): kwargs[arg] = separators[fmt].join( "{}".format(item) for item in value From c4bea9c30ecc7232566679410d695ef13825c1ed Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Thu, 10 Oct 2019 10:40:44 +1300 Subject: [PATCH 2/2] Simplify check on whether format is in the separators list --- pygmt/helpers/decorators.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pygmt/helpers/decorators.py b/pygmt/helpers/decorators.py index 47bd4377362..fb6fd475347 100644 --- a/pygmt/helpers/decorators.py +++ b/pygmt/helpers/decorators.py @@ -286,12 +286,7 @@ def new_module(*args, **kwargs): for arg, fmt in conversions.items(): if arg in kwargs: value = kwargs[arg] - issequence = fmt in ( - "sequence", - "sequence_comma", - "sequence_plus", - "sequence_space", - ) + issequence = fmt in separators if issequence and is_nonstr_iter(value): kwargs[arg] = separators[fmt].join( "{}".format(item) for item in value