-
Notifications
You must be signed in to change notification settings - Fork 229
New Alias System: Add the Alias class #3993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces the Alias
class as a dataclass-based wrapper for the existing _to_string
function to establish the foundation for a new alias system. The class provides a more structured and object-oriented approach to handling parameter aliasing between PyGMT's long-form arguments and GMT's short-form options.
- Adds the
Alias
dataclass with attributes matching_to_string
parameters - Implements a
_value
property that delegates to the existing_to_string
function - Provides comprehensive documentation with usage examples
Comments suppressed due to low confidence (1)
pygmt/alias.py:185
- [nitpick] The property name '_value' uses a leading underscore which typically indicates a private/internal attribute, but this appears to be the main interface for accessing the converted value. Consider using 'value' or 'converted_value' for better API clarity.
def _value(self) -> str | list[str] | None:
pygmt/alias.py
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's just a simple class with one property, I'm wondering if we should avoid using dataclass
and instead write it like:
class Alias:
def __init__(value, name=None, prefix="", ....):
self.value = value
self.name = name
self.prefix = prefix
...
self._value = _to_string(
value=self.value,
name=self.name,
prefix=self.prefix,
mapping=self.mapping,
separator=self.separator,
size=self.size,
ndim=self.ndim,
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plain class is about 30% faster than dataclass. Considering that the Alias class is used extensively in the project, I've rewritten it in 4cdef8b.
This PR adds the
Alias
class, which wraps the _to_string function and is responsible for converting any value into a string or a list of strings.It's the basis of the new alias system (#4000) and class-like parameters (see #3995). So, please review PRs #3995 and #4000 first.