1+ """
2+ Alias system to convert PyGMT parameters to GMT options.
3+ """
14import inspect
25from collections import defaultdict
36from typing import NamedTuple
69
710
811class Alias (NamedTuple ):
12+ """
13+ Alias system for mapping a PyGMT parameter to its equivalent GMT option string.
14+
15+ Attributes
16+ ----------
17+ name : str
18+ PyGMT parameter name.
19+ flag : str
20+ GMT single-letter option flag.
21+ modifier : str
22+ GMT option modifier. Can be None.
23+ separator : str
24+ Separator to join the iterable argument into a string.
25+ """
26+
927 name : str
1028 flag : str
1129 modifier : str
1230 separator : str
1331
1432
1533def sequence_to_str (seq , separator ):
34+ """
35+ Join a sequence (list, tuple) into a string with the specified separator.
36+
37+ Examples
38+ --------
39+ >>> sequence_to_str((1, 2, 3), "/")
40+ '1/2/3'
41+ >>> sequence_to_str((1, 2, 3), ",")
42+ '1,2,3'
43+ >>> sequence_to_str((1, 2, 3), " ")
44+ '1 2 3'
45+ """
1646 return separator .join (str (item ) for item in seq )
1747
1848
@@ -21,13 +51,39 @@ def convert_aliases():
2151 Convert PyGMT parameters to GMT options.
2252
2353 The caller function must have the special variable ``_aliases`` defined.
54+
55+ Examples
56+ --------
57+ >>> def module_func(**kwargs):
58+ ... _aliases = [
59+ ... Alias("par1", "A", "", ""),
60+ ... Alias("par2", "B", "", "/"),
61+ ... Alias("par3", "C", "", ","),
62+ ... Alias("pard1", "D", "", ""),
63+ ... Alias("pard2", "D", "+a", ""),
64+ ... Alias("pard3", "D", "+b", ","),
65+ ... Alias("pard4", "D", "+c", "/"),
66+ ... ]
67+ ... options = convert_aliases()
68+ ... print(options)
69+ >>>
70+ >>> module_func(
71+ ... par1="value1",
72+ ... par2=[1, 2, 3, 4],
73+ ... par3=[0, 1],
74+ ... pard1="value2",
75+ ... pard2="value3",
76+ ... pard3=[1, 2, 3, 4],
77+ ... pard4=[1, 2, 3, 4],
78+ ... )
79+ {'A': 'value1', 'B': '1/2/3/4', 'C': '0,1', 'D': 'value2+avalue3+b1,2,3,4+c1/2/3/4'}
2480 """
2581 # Get the local namespace of the caller function
2682 p_locals = inspect .currentframe ().f_back .f_locals
2783 params = p_locals .pop ("kwargs" , {}) | p_locals
2884
2985 # Define a dict to store GMT option flags and arguments
30- options = defaultdict (lambda : "" )
86+ kwdict = defaultdict (lambda : "" ) # use defaultdict to avoid KeyError
3187 for alias in p_locals .get ("_aliases" ):
3288 value = params .get (alias .name )
3389 if is_nonstr_iter (value ):
@@ -39,5 +95,5 @@ def convert_aliases():
3995 continue
4096 elif value is True : # Convert True to an empty string
4197 value = ""
42- options [alias .flag ] += f"{ alias .modifier } { value } "
43- return dict (options )
98+ kwdict [alias .flag ] += f"{ alias .modifier } { value } "
99+ return dict (kwdict )
0 commit comments