Skip to content

Commit 1a26f96

Browse files
committed
Implement a general BaseParams class for complex options
1 parent 048700c commit 1a26f96

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

pygmt/params.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Iterable, Sequence
4+
from dataclasses import dataclass
5+
from typing import NamedTuple
6+
7+
8+
class Alias(NamedTuple):
9+
name: str
10+
modifier: str
11+
separator: str | None = None
12+
13+
14+
class BaseParams:
15+
def __str__(self):
16+
values = []
17+
for alias in self.aliases:
18+
value = getattr(self, alias.name)
19+
if value in (None, False):
20+
continue
21+
if value is True:
22+
value = ""
23+
if isinstance(value, Iterable) and not isinstance(value, str):
24+
value = alias.separator.join(map(str, value))
25+
values.append(f"{alias.modifier}{value}")
26+
return "".join(values)
27+
28+
def __repr__(self):
29+
string = []
30+
for alias in self.aliases:
31+
value = getattr(self, alias.name)
32+
if value is None or value is False:
33+
continue
34+
string.append(f"{alias.name}={value!r}")
35+
return f"{self.__class__.__name__}({', '.join(string)})"
36+
37+
38+
@dataclass(repr=False)
39+
class Box(BaseParams):
40+
clearance: float | str | Sequence[float | str] | None = None
41+
fill: str | None = None
42+
innerborder: str | Sequence | None = None
43+
pen: str | None = None
44+
radius: float | bool | None = False
45+
shading: str | Sequence | None = None
46+
47+
aliases = [
48+
Alias("clearance", "+c", "/"),
49+
Alias("fill", "+g"),
50+
Alias("innerborder", "+i", "/"),
51+
Alias("pen", "+p"),
52+
Alias("radius", "+r"),
53+
Alias("shading", "+s", "/"),
54+
]

pygmt/src/logo.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
"""
22
logo - Plot the GMT logo
33
"""
4+
from __future__ import annotations
5+
6+
from typing import TYPE_CHECKING
7+
8+
if TYPE_CHECKING:
9+
from pygmt.params import Box
410

511
from pygmt.clib import Session
612
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
@@ -18,7 +24,7 @@
1824
t="transparency",
1925
)
2026
@kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence")
21-
def logo(self, **kwargs):
27+
def logo(self, box: bool | Box = False, **kwargs):
2228
r"""
2329
Plot the GMT logo.
2430
@@ -39,7 +45,7 @@ def logo(self, **kwargs):
3945
[**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\
4046
**+w**\ *width*\ [**+j**\ *justify*]\ [**+o**\ *dx*\ [/*dy*]].
4147
Set reference point on the map for the image.
42-
box : bool or str
48+
box
4349
If set to ``True``, draw a rectangular border around the
4450
GMT logo.
4551
style : str

0 commit comments

Comments
 (0)