Skip to content

Commit 7ae8500

Browse files
committed
Simplify the AliasSystem class
1 parent c893755 commit 7ae8500

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

pygmt/alias.py

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,15 @@ def _value(self) -> str | Sequence[str] | None:
155155

156156
class AliasSystem:
157157
"""
158-
Alias system to convert PyGMT parameter into a keyword dictionary for GMT options.
158+
Alias system for converting PyGMT parameters to GMT options.
159159
160-
The AliasSystem class is initialized by keyword arguments where the key is the GMT
161-
single-letter option flag and the value is one or a list of ``Alias`` objects.
160+
The AliasSystem class is initialized with keyword arguments, where each key is a GMT
161+
option flag, and the corresponding value is an ``Alias`` object or a list of
162+
``Alias`` objects.
162163
163-
The ``kwdict`` property is a keyword dictionary that stores the current parameter
164-
values. The key of the dictionary is the GMT single-letter option flag, and the
165-
value is the corresponding value of the option. The value can be a string or a
166-
sequence of strings, or None. The keyword dictionary can be passed to the
167-
``build_arg_list`` function.
168-
169-
Need to note that the ``kwdict`` property is dynamically computed from the current
170-
values of parameters. So, don't change it and avoid accessing it multiple times.
164+
The class provides the ``kwdict`` attribute, which is a dictionary mapping each GMT
165+
option flag to its current value. The value can be a string or a list of strings.
166+
This keyword dictionary can then be passed to the ``build_arg_list`` function.
171167
172168
Examples
173169
--------
@@ -207,33 +203,24 @@ class AliasSystem:
207203

208204
def __init__(self, **kwargs):
209205
"""
210-
Initialize as a dictionary of GMT options and their aliases.
206+
Initialize the alias system and create the keyword dictionary that stores the
207+
current parameter values.
211208
"""
212-
self.options = {}
213-
for option, aliases in kwargs.items():
214-
match aliases:
215-
case list():
216-
self.options[option] = aliases
217-
case _:
218-
self.options[option] = [aliases]
209+
# Keyword dictionary with an empty string as default value.
210+
self.kwdict = defaultdict(str)
219211

220-
@property
221-
def kwdict(self):
222-
"""
223-
A keyword dictionary that stores the current parameter values.
224-
"""
225-
# Default value is an empty string to simplify code logic.
226-
kwdict = defaultdict(str)
227-
for option, aliases in self.options.items():
228-
for alias in aliases:
229-
# value can be a string, a sequence of strings or None.
230-
if alias._value is None:
231-
continue
232-
# Special handing of repeatable parameter like -B/frame.
233-
if is_nonstr_iter(alias._value):
234-
kwdict[option] = alias._value
235-
# A repeatable option should have only one alias, so break.
236-
break
237-
238-
kwdict[option] += alias._value
239-
return kwdict
212+
for option, aliases in kwargs.items():
213+
if not is_nonstr_iter(aliases): # Single alias.
214+
self.kwdict[option] = aliases._value
215+
continue
216+
217+
for alias in aliases: # List of aliases.
218+
match alias._value:
219+
case None:
220+
continue
221+
case str():
222+
self.kwdict[option] += alias._value
223+
case list():
224+
# A repeatable option should have only one alias, so break.
225+
self.kwdict[option] = alias._value
226+
break

0 commit comments

Comments
 (0)