Skip to content

Commit 654261c

Browse files
committed
New Alias System: Add the Alias class
1 parent 7a682ff commit 654261c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

pygmt/alias.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
The PyGMT alias system to convert PyGMT's long-form arguments to GMT's short-form.
33
"""
44

5+
import dataclasses
56
from collections.abc import Mapping, Sequence
67
from typing import Any, Literal
78

@@ -131,3 +132,66 @@ def _to_string(
131132
# "prefix" and "mapping" are ignored. We can enable them when needed.
132133
_value = sequence_join(value, separator=separator, size=size, ndim=ndim, name=name)
133134
return f"{prefix}{_value}"
135+
136+
137+
@dataclasses.dataclass
138+
class Alias:
139+
"""
140+
Class for aliasing a PyGMT parameter to a GMT option or a modifier.
141+
142+
Attributes
143+
----------
144+
value
145+
The value of the alias.
146+
prefix
147+
The string to add as a prefix to the returned value.
148+
mapping
149+
A mapping dictionary to map PyGMT's long-form arguments to GMT's short-form.
150+
separator
151+
The separator to use if the value is a sequence.
152+
size
153+
Expected size of the 1-D sequence. It can be either an integer or a sequence of
154+
integers. If an integer, it is the expected size of the 1-D sequence. If it is a
155+
sequence, it is the allowed sizes of the 1-D sequence.
156+
ndim
157+
The expected maximum number of dimensions of the sequence.
158+
name
159+
The name of the parameter to be used in the error message.
160+
161+
Examples
162+
--------
163+
>>> par = Alias((3.0, 3.0), prefix="+o", separator="/")
164+
>>> par._value
165+
'+o3.0/3.0'
166+
167+
>>> par = Alias("mean", mapping={"mean": "a", "mad": "d", "full": "g"})
168+
>>> par._value
169+
'a'
170+
171+
>>> par = Alias(["xaf", "yaf", "WSen"])
172+
>>> par._value
173+
['xaf', 'yaf', 'WSen']
174+
"""
175+
176+
value: Any
177+
prefix: str = ""
178+
mapping: Mapping | None = None
179+
separator: Literal["/", ","] | None = None
180+
size: int | Sequence[int] | None = None
181+
ndim: int = 1
182+
name: str | None = None
183+
184+
@property
185+
def _value(self) -> str | list[str] | None:
186+
"""
187+
The value of the alias as a string, a sequence of strings or None.
188+
"""
189+
return _to_string(
190+
value=self.value,
191+
prefix=self.prefix,
192+
mapping=self.mapping,
193+
separator=self.separator,
194+
size=self.size,
195+
ndim=self.ndim,
196+
name=self.name,
197+
)

0 commit comments

Comments
 (0)