From 654261c277eeadac7fe6da8f80d1e0f9c75bfc0f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 11 Jul 2025 00:51:16 +0800 Subject: [PATCH 1/5] New Alias System: Add the Alias class --- pygmt/alias.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/pygmt/alias.py b/pygmt/alias.py index 3d46dd11b82..13e94079291 100644 --- a/pygmt/alias.py +++ b/pygmt/alias.py @@ -2,6 +2,7 @@ The PyGMT alias system to convert PyGMT's long-form arguments to GMT's short-form. """ +import dataclasses from collections.abc import Mapping, Sequence from typing import Any, Literal @@ -131,3 +132,66 @@ def _to_string( # "prefix" and "mapping" are ignored. We can enable them when needed. _value = sequence_join(value, separator=separator, size=size, ndim=ndim, name=name) return f"{prefix}{_value}" + + +@dataclasses.dataclass +class Alias: + """ + Class for aliasing a PyGMT parameter to a GMT option or a modifier. + + Attributes + ---------- + value + The value of the alias. + prefix + The string to add as a prefix to the returned value. + mapping + A mapping dictionary to map PyGMT's long-form arguments to GMT's short-form. + separator + The separator to use if the value is a sequence. + size + Expected size of the 1-D sequence. It can be either an integer or a sequence of + integers. If an integer, it is the expected size of the 1-D sequence. If it is a + sequence, it is the allowed sizes of the 1-D sequence. + ndim + The expected maximum number of dimensions of the sequence. + name + The name of the parameter to be used in the error message. + + Examples + -------- + >>> par = Alias((3.0, 3.0), prefix="+o", separator="/") + >>> par._value + '+o3.0/3.0' + + >>> par = Alias("mean", mapping={"mean": "a", "mad": "d", "full": "g"}) + >>> par._value + 'a' + + >>> par = Alias(["xaf", "yaf", "WSen"]) + >>> par._value + ['xaf', 'yaf', 'WSen'] + """ + + value: Any + prefix: str = "" + mapping: Mapping | None = None + separator: Literal["/", ","] | None = None + size: int | Sequence[int] | None = None + ndim: int = 1 + name: str | None = None + + @property + def _value(self) -> str | list[str] | None: + """ + The value of the alias as a string, a sequence of strings or None. + """ + return _to_string( + value=self.value, + prefix=self.prefix, + mapping=self.mapping, + separator=self.separator, + size=self.size, + ndim=self.ndim, + name=self.name, + ) From 7f43055889abd805e8f0712858ea845d752e2917 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 16 Jul 2025 09:14:30 +0800 Subject: [PATCH 2/5] Fix --- pygmt/alias.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/alias.py b/pygmt/alias.py index 13e94079291..0267fcc33c5 100644 --- a/pygmt/alias.py +++ b/pygmt/alias.py @@ -152,7 +152,7 @@ class Alias: size Expected size of the 1-D sequence. It can be either an integer or a sequence of integers. If an integer, it is the expected size of the 1-D sequence. If it is a - sequence, it is the allowed sizes of the 1-D sequence. + sequence, it is the allowed size of the 1-D sequence. ndim The expected maximum number of dimensions of the sequence. name From fae3ab01b09ec9a3def8a75f783f00692917c38b Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 21 Jul 2025 18:42:16 +0800 Subject: [PATCH 3/5] Put name after value --- pygmt/alias.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/alias.py b/pygmt/alias.py index a6e02dc0c4a..43fcc3c202e 100644 --- a/pygmt/alias.py +++ b/pygmt/alias.py @@ -143,6 +143,8 @@ class Alias: ---------- value The value of the alias. + name + The name of the parameter to be used in the error message. prefix The string to add as a prefix to the returned value. mapping @@ -155,8 +157,6 @@ class Alias: sequence, it is the allowed size of the 1-D sequence. ndim The expected maximum number of dimensions of the sequence. - name - The name of the parameter to be used in the error message. Examples -------- @@ -174,12 +174,12 @@ class Alias: """ value: Any + name: str | None = None prefix: str = "" mapping: Mapping | None = None separator: Literal["/", ","] | None = None size: int | Sequence[int] | None = None ndim: int = 1 - name: str | None = None @property def _value(self) -> str | list[str] | None: @@ -188,10 +188,10 @@ def _value(self) -> str | list[str] | None: """ return _to_string( value=self.value, + name=self.name, prefix=self.prefix, mapping=self.mapping, separator=self.separator, size=self.size, ndim=self.ndim, - name=self.name, ) From 4cdef8b62bf8d51aed25e5dead727ab7e5975771 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 28 Jul 2025 17:52:22 +0800 Subject: [PATCH 4/5] Avoid using dataclass --- pygmt/alias.py | 56 ++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/pygmt/alias.py b/pygmt/alias.py index 43fcc3c202e..92f44120696 100644 --- a/pygmt/alias.py +++ b/pygmt/alias.py @@ -2,7 +2,6 @@ The PyGMT alias system to convert PyGMT's long-form arguments to GMT's short-form. """ -import dataclasses from collections.abc import Mapping, Sequence from typing import Any, Literal @@ -134,12 +133,11 @@ def _to_string( return f"{prefix}{_value}" -@dataclasses.dataclass class Alias: """ Class for aliasing a PyGMT parameter to a GMT option or a modifier. - Attributes + Parameters ---------- value The value of the alias. @@ -152,9 +150,9 @@ class Alias: separator The separator to use if the value is a sequence. size - Expected size of the 1-D sequence. It can be either an integer or a sequence of - integers. If an integer, it is the expected size of the 1-D sequence. If it is a - sequence, it is the allowed size of the 1-D sequence. + Expected size of the 1-D sequence. It can be either an integer or a sequence + of integers. If an integer, it is the expected size of the 1-D sequence. + If it is a sequence, it is the allowed sizes of the 1-D sequence. ndim The expected maximum number of dimensions of the sequence. @@ -173,25 +171,29 @@ class Alias: ['xaf', 'yaf', 'WSen'] """ - value: Any - name: str | None = None - prefix: str = "" - mapping: Mapping | None = None - separator: Literal["/", ","] | None = None - size: int | Sequence[int] | None = None - ndim: int = 1 - - @property - def _value(self) -> str | list[str] | None: - """ - The value of the alias as a string, a sequence of strings or None. - """ - return _to_string( - value=self.value, - name=self.name, - prefix=self.prefix, - mapping=self.mapping, - separator=self.separator, - size=self.size, - ndim=self.ndim, + def __init__( + self, + value: Any, + name: str | None = None, + prefix: str = "", + mapping: Mapping | None = None, + separator: Literal["/", ","] | None = None, + size: int | Sequence[int] | None = None, + ndim: int = 1, + ): + self.value = value + self.name = name + self.prefix = prefix + self.mapping = mapping + self.separator = separator + self.size = size + self.ndim = ndim + self._value = _to_string( + value=value, + name=name, + prefix=prefix, + mapping=mapping, + separator=separator, + size=size, + ndim=ndim, ) From e57954d326cc37e8228d40c17e266abee2a74827 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 28 Jul 2025 21:00:33 +0800 Subject: [PATCH 5/5] No need to store properties that won't be used --- pygmt/alias.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pygmt/alias.py b/pygmt/alias.py index 92f44120696..3011f4b2f2d 100644 --- a/pygmt/alias.py +++ b/pygmt/alias.py @@ -181,13 +181,8 @@ def __init__( size: int | Sequence[int] | None = None, ndim: int = 1, ): - self.value = value self.name = name self.prefix = prefix - self.mapping = mapping - self.separator = separator - self.size = size - self.ndim = ndim self._value = _to_string( value=value, name=name,