1
+ """
2
+ Alias system to convert PyGMT parameters to GMT options.
3
+ """
1
4
import inspect
2
5
from collections import defaultdict
3
6
from typing import NamedTuple
6
9
7
10
8
11
class 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
+
9
27
name : str
10
28
flag : str
11
29
modifier : str
12
30
separator : str
13
31
14
32
15
33
def 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
+ """
16
46
return separator .join (str (item ) for item in seq )
17
47
18
48
@@ -21,13 +51,39 @@ def convert_aliases():
21
51
Convert PyGMT parameters to GMT options.
22
52
23
53
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'}
24
80
"""
25
81
# Get the local namespace of the caller function
26
82
p_locals = inspect .currentframe ().f_back .f_locals
27
83
params = p_locals .pop ("kwargs" , {}) | p_locals
28
84
29
85
# Define a dict to store GMT option flags and arguments
30
- options = defaultdict (lambda : "" )
86
+ kwdict = defaultdict (lambda : "" ) # use defaultdict to avoid KeyError
31
87
for alias in p_locals .get ("_aliases" ):
32
88
value = params .get (alias .name )
33
89
if is_nonstr_iter (value ):
@@ -39,5 +95,5 @@ def convert_aliases():
39
95
continue
40
96
elif value is True : # Convert True to an empty string
41
97
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