|
| 1 | +""" |
| 2 | +Base class for common parameters shared in PyGMT. |
| 3 | +""" |
| 4 | + |
| 5 | +from abc import ABC, abstractmethod |
| 6 | + |
| 7 | + |
| 8 | +class BaseParam(ABC): |
| 9 | + """ |
| 10 | + Base class for parameters in PyGMT. |
| 11 | +
|
| 12 | + To define a new parameter class, inherit from this class and define the attributes |
| 13 | + that correspond to the parameters you want to include. |
| 14 | +
|
| 15 | + The class should also implement the ``_aliases`` property, which returns a list of |
| 16 | + ``Alias`` objects. Each ``Alias`` object represents a parameter and its value, and |
| 17 | + the ``__str__`` method will concatenate these values into a single string that can |
| 18 | + be passed to GMT. |
| 19 | +
|
| 20 | + Optionally, you can override the ``_validate`` method to perform any necessary |
| 21 | + validation on the parameters after initialization. |
| 22 | +
|
| 23 | + Examples |
| 24 | + -------- |
| 25 | + >>> from typing import Any |
| 26 | + >>> import dataclasses |
| 27 | + >>> from pygmt.params.base import BaseParam |
| 28 | + >>> from pygmt.alias import Alias |
| 29 | + >>> |
| 30 | + >>> @dataclasses.dataclass(repr=False) |
| 31 | + ... class Test(BaseParam): |
| 32 | + ... par1: Any = None |
| 33 | + ... par2: Any = None |
| 34 | + ... par3: Any = None |
| 35 | + ... |
| 36 | + ... @property |
| 37 | + ... def _aliases(self): |
| 38 | + ... return [ |
| 39 | + ... Alias(self.par1), |
| 40 | + ... Alias(self.par2, prefix="+a"), |
| 41 | + ... Alias(self.par3, prefix="+b", sep="/"), |
| 42 | + ... ] |
| 43 | +
|
| 44 | + >>> var = Test(par1="val1") |
| 45 | + >>> str(var) |
| 46 | + 'val1' |
| 47 | + >>> repr(var) |
| 48 | + "Test(par1='val1')" |
| 49 | +
|
| 50 | + >>> var = Test(par1="val1", par2="val2", par3=("val3a", "val3b")) |
| 51 | + >>> str(var) |
| 52 | + 'val1+aval2+bval3a/val3b' |
| 53 | + >>> repr(var) |
| 54 | + "Test(par1='val1', par2='val2', par3=('val3a', 'val3b'))" |
| 55 | + """ |
| 56 | + |
| 57 | + def __post_init__(self): |
| 58 | + """ |
| 59 | + Post-initialization method to _validate the _aliases property. |
| 60 | + """ |
| 61 | + self._validate() |
| 62 | + |
| 63 | + def _validate(self): # noqa: B027 |
| 64 | + """ |
| 65 | + Validate the parameters of the object. |
| 66 | +
|
| 67 | + Optional method but can be overridden in subclasses to perform any necessary |
| 68 | + validation on the parameters. |
| 69 | + """ |
| 70 | + |
| 71 | + @property |
| 72 | + @abstractmethod |
| 73 | + def _aliases(self): |
| 74 | + """ |
| 75 | + List of Alias objects representing the parameters of this class. |
| 76 | +
|
| 77 | + Must be implemented in subclasses to define the parameters and their aliases. |
| 78 | + """ |
| 79 | + |
| 80 | + def __str__(self): |
| 81 | + """ |
| 82 | + String representation of the object that can be passed to GMT directly. |
| 83 | + """ |
| 84 | + return "".join( |
| 85 | + [alias._value for alias in self._aliases if alias._value is not None] |
| 86 | + ) |
| 87 | + |
| 88 | + def __repr__(self): |
| 89 | + """ |
| 90 | + String representation of the object. |
| 91 | + """ |
| 92 | + params = ", ".join( |
| 93 | + f"{k}={v!r}" |
| 94 | + for k, v in vars(self).items() |
| 95 | + if v is not None and v is not False |
| 96 | + ) |
| 97 | + return f"{self.__class__.__name__}({params})" |
0 commit comments