Skip to content

Commit 210292f

Browse files
committed
Add validate method
1 parent 48702b5 commit 210292f

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

pygmt/params/base.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,19 @@ class BaseParam:
4747
"Test(par1='val1', par2='val2', par3=('val3a', 'val3b'))"
4848
"""
4949

50-
def __str__(self):
50+
def __post_init__(self):
5151
"""
52-
String representation of the object that can be passed to GMT directly.
52+
Post-initialization method to _validate the _aliases property.
5353
"""
54-
return "".join(
55-
[alias._value for alias in self._aliases if alias._value is not None]
56-
)
54+
self.validate()
5755

58-
def __repr__(self):
56+
def validate(self):
5957
"""
60-
String representation of the object.
58+
Validate the parameters of the object.
59+
60+
This method should be overridden in subclasses to perform any necessary
61+
validation on the parameters.
6162
"""
62-
params = ", ".join(f"{k}={v!r}" for k, v in vars(self).items() if v is not None)
63-
return f"{self.__class__.__name__}({params})"
6463

6564
@property
6665
def _aliases(self):
@@ -72,3 +71,18 @@ def _aliases(self):
7271
"""
7372
msg = "The _aliases property must be implemented in subclasses."
7473
raise NotImplementedError(msg)
74+
75+
def __str__(self):
76+
"""
77+
String representation of the object that can be passed to GMT directly.
78+
"""
79+
return "".join(
80+
[alias._value for alias in self._aliases if alias._value is not None]
81+
)
82+
83+
def __repr__(self):
84+
"""
85+
String representation of the object.
86+
"""
87+
params = ", ".join(f"{k}={v!r}" for k, v in vars(self).items() if v is not None)
88+
return f"{self.__class__.__name__}({params})"

pygmt/params/box.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ class Box(BaseParam):
8181
shading_offset: Sequence[float | str] | None = None
8282
shading_fill: str | None = None
8383

84+
def validate(self):
85+
"""
86+
Validate the parameters.
87+
"""
88+
# shading_offset must be a sequence of two values or None.
89+
if (
90+
self.shading_offset and not isinstance(self.shading_offset, Sequence)
91+
) or len(self.shading_offset) != 2:
92+
raise GMTValueError(
93+
self.shading_offset,
94+
description="value for parameter 'shading_offset'",
95+
reason="Must be a sequence of two values (dx, dy) or None.",
96+
)
97+
8498
@property
8599
def _innerborder(self) -> list[str | float] | None:
86100
"""
@@ -93,16 +107,7 @@ def _shading(self) -> list[str | float] | None:
93107
"""
94108
Shading for the box, formatted as a list of 1-3 values, or None.
95109
"""
96-
# Local variable to simplify the code.
97-
_shading_offset = [] if self.shading_offset is None else self.shading_offset
98-
99-
# shading_offset must be a sequence of two values (dx, dy) or None.
100-
if len(_shading_offset) not in {0, 2}:
101-
raise GMTValueError(
102-
self.shading_offset,
103-
description="value for parameter 'shading_offset'",
104-
reason="Must be a sequence of two values (dx, dy) or None.",
105-
)
110+
_shading_offset = self.shading_offset or []
106111
return [
107112
v for v in (*_shading_offset, self.shading_fill) if v is not None
108113
] or None

0 commit comments

Comments
 (0)