Skip to content

Commit 72bf183

Browse files
committed
Updates
1 parent b6a8ce9 commit 72bf183

File tree

7 files changed

+68
-12
lines changed

7 files changed

+68
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test_no_images: PYTEST_ARGS=-o addopts="--verbose --durations=0 --durations-min=
6060
test_no_images: _runtest
6161

6262
format:
63-
ruff check --fix $(FORMAT_FILES)
63+
ruff check --fix --exit-zero $(FORMAT_FILES)
6464
ruff format $(FORMAT_FILES)
6565

6666
check:

pygmt/alias.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Alias system to convert PyGMT parameters to GMT options.
3+
"""
14
import inspect
25
from collections import defaultdict
36
from typing import NamedTuple
@@ -6,13 +9,40 @@
69

710

811
class Alias(NamedTuple):
12+
"""
13+
Alias system for mapping a PyGMT parameter to its equivalent GMT option string.
14+
15+
Attributes
16+
----------
17+
name : str
18+
PyGMT parameter name.
19+
flag : str
20+
GMT single-letter option flag.
21+
modifier : str
22+
GMT option modifier. Can be None.
23+
separator : str
24+
Separator to join the iterable argument into a string.
25+
"""
26+
927
name: str
1028
flag: str
1129
modifier: str
1230
separator: str
1331

1432

1533
def sequence_to_str(seq, separator):
34+
"""
35+
Join a sequence (list, tuple) into a string with the specified separator.
36+
37+
Examples
38+
--------
39+
>>> sequence_to_str((1, 2, 3), "/")
40+
'1/2/3'
41+
>>> sequence_to_str((1, 2, 3), ",")
42+
'1,2,3'
43+
>>> sequence_to_str((1, 2, 3), " ")
44+
'1 2 3'
45+
"""
1646
return separator.join(str(item) for item in seq)
1747

1848

@@ -21,13 +51,39 @@ def convert_aliases():
2151
Convert PyGMT parameters to GMT options.
2252
2353
The caller function must have the special variable ``_aliases`` defined.
54+
55+
Examples
56+
--------
57+
>>> def module_func(**kwargs):
58+
... _aliases = [
59+
... Alias("par1", "A", "", ""),
60+
... Alias("par2", "B", "", "/"),
61+
... Alias("par3", "C", "", ","),
62+
... Alias("pard1", "D", "", ""),
63+
... Alias("pard2", "D", "+a", ""),
64+
... Alias("pard3", "D", "+b", ","),
65+
... Alias("pard4", "D", "+c", "/"),
66+
... ]
67+
... options = convert_aliases()
68+
... print(options)
69+
>>>
70+
>>> module_func(
71+
... par1="value1",
72+
... par2=[1, 2, 3, 4],
73+
... par3=[0, 1],
74+
... pard1="value2",
75+
... pard2="value3",
76+
... pard3=[1, 2, 3, 4],
77+
... pard4=[1, 2, 3, 4],
78+
... )
79+
{'A': 'value1', 'B': '1/2/3/4', 'C': '0,1', 'D': 'value2+avalue3+b1,2,3,4+c1/2/3/4'}
2480
"""
2581
# Get the local namespace of the caller function
2682
p_locals = inspect.currentframe().f_back.f_locals
2783
params = p_locals.pop("kwargs", {}) | p_locals
2884

2985
# Define a dict to store GMT option flags and arguments
30-
options = defaultdict(lambda: "")
86+
kwdict = defaultdict(lambda: "") # use defaultdict to avoid KeyError
3187
for alias in p_locals.get("_aliases"):
3288
value = params.get(alias.name)
3389
if is_nonstr_iter(value):
@@ -39,5 +95,5 @@ def convert_aliases():
3995
continue
4096
elif value is True: # Convert True to an empty string
4197
value = ""
42-
options[alias.flag] += f"{alias.modifier}{value}"
43-
return dict(options)
98+
kwdict[alias.flag] += f"{alias.modifier}{value}"
99+
return dict(kwdict)

pygmt/clib/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ def create(self, name):
371371
self._error_log = []
372372

373373
@ctp.CFUNCTYPE(ctp.c_int, ctp.c_void_p, ctp.c_char_p)
374-
def print_func(file_pointer, message): # noqa: ARG001
374+
def print_func(file_pointer, message):
375375
"""
376376
Callback function that the GMT C API will use to print log and error
377377
messages.

pygmt/sphinx_gallery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PyGMTScraper:
1919
``conf.py`` as the ``"image_scrapers"`` argument.
2020
"""
2121

22-
def __call__(self, block, block_vars, gallery_conf): # noqa: ARG002
22+
def __call__(self, block, block_vars, gallery_conf):
2323
"""
2424
Called by sphinx-gallery to save the figures generated after running code.
2525
"""

pygmt/tests/test_clib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def mock(session, func, returns=None, mock_func=None):
3838
"""
3939
if mock_func is None:
4040

41-
def mock_api_function(*args): # noqa: ARG001
41+
def mock_api_function(*args):
4242
"""
4343
A mock GMT API function that always returns a given value.
4444
"""
@@ -525,7 +525,7 @@ def test_info_dict():
525525
assert lib.info
526526

527527
# Mock GMT_Get_Default to return always the same string
528-
def mock_defaults(api, name, value): # noqa: ARG001
528+
def mock_defaults(api, name, value):
529529
"""
530530
Put 'bla' in the value buffer.
531531
"""
@@ -554,7 +554,7 @@ def test_fails_for_wrong_version():
554554
"""
555555

556556
# Mock GMT_Get_Default to return an old version
557-
def mock_defaults(api, name, value): # noqa: ARG001
557+
def mock_defaults(api, name, value):
558558
"""
559559
Return an old version.
560560
"""

pygmt/tests/test_clib_loading.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_load_libgmt_fails(monkeypatch):
8585
mpatch.setattr(
8686
subprocess,
8787
"check_output",
88-
lambda cmd, encoding: "libfakegmt.so", # noqa: ARG005
88+
lambda cmd, encoding: "libfakegmt.so",
8989
)
9090
with pytest.raises(GMTCLibNotFoundError):
9191
check_libgmt(load_libgmt())
@@ -220,7 +220,7 @@ class TestLibgmtCount:
220220
loaded_libgmt = load_libgmt() # Load the GMT library and reuse it when necessary
221221
counter = 0 # Global counter for how many times ctypes.CDLL is called
222222

223-
def _mock_ctypes_cdll_return(self, libname): # noqa: ARG002
223+
def _mock_ctypes_cdll_return(self, libname):
224224
"""
225225
Mock ctypes.CDLL to count how many times the function is called.
226226

pygmt/tests/test_figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def test_figure_savefig():
224224
"""
225225
kwargs_saved = []
226226

227-
def mock_psconvert(*args, **kwargs): # noqa: ARG001
227+
def mock_psconvert(*args, **kwargs):
228228
"""
229229
Just record the arguments.
230230
"""

0 commit comments

Comments
 (0)