Skip to content

Commit b6707b1

Browse files
committed
Refactor the Alias class
1 parent 145dd79 commit b6707b1

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

pygmt/alias.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,29 @@ class Alias:
127127
Examples
128128
--------
129129
>>> par = Alias((3.0, 3.0), prefix="+o", separator="/")
130-
>>> par.value
130+
>>> par._value
131131
'+o3.0/3.0'
132132
133133
>>> par = Alias(["xaf", "yaf", "WSen"])
134-
>>> par.value
134+
>>> par._value
135135
['xaf', 'yaf', 'WSen']
136136
"""
137137

138-
def __init__(
139-
self,
140-
value: Any,
141-
prefix: str = "",
142-
separator: Literal["/", ","] | None = None,
143-
mapping: bool | Mapping = False,
144-
):
145-
self.value = to_string(
146-
value=value, prefix=prefix, separator=separator, mapping=mapping
138+
value: Any
139+
prefix: str = ""
140+
separator: Literal["/", ","] | None = None
141+
mapping: bool | Mapping = False
142+
143+
@property
144+
def _value(self) -> str | Sequence[str] | None:
145+
"""
146+
The value of the alias as a string, a sequence of strings or None.
147+
"""
148+
return to_string(
149+
value=self.value,
150+
prefix=self.prefix,
151+
separator=self.separator,
152+
mapping=self.mapping,
147153
)
148154

149155

@@ -221,13 +227,13 @@ def kwdict(self):
221227
for option, aliases in self.options.items():
222228
for alias in aliases:
223229
# value can be a string, a sequence of strings or None.
224-
if alias.value is None:
230+
if alias._value is None:
225231
continue
226232
# Special handing of repeatable parameter like -B/frame.
227-
if is_nonstr_iter(alias.value):
228-
kwdict[option] = alias.value
233+
if is_nonstr_iter(alias._value):
234+
kwdict[option] = alias._value
229235
# A repeatable option should have only one alias, so break.
230236
break
231237

232-
kwdict[option] += alias.value
238+
kwdict[option] += alias._value
233239
return kwdict

pygmt/params/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __str__(self):
3939
String representation of the object that can be passed to GMT directly.
4040
"""
4141
return "".join(
42-
[alias.value for alias in self._aliases if alias.value is not None]
42+
[alias._value for alias in self._aliases if alias._value is not None]
4343
)
4444

4545
def __repr__(self):

pygmt/params/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,4 @@ def __iter__(self):
9393
------
9494
The value of each alias in the class. None are excluded.
9595
"""
96-
yield from (alias.value for alias in self._aliases if alias.value is not None)
96+
yield from (alias._value for alias in self._aliases if alias._value is not None)

0 commit comments

Comments
 (0)