Skip to content

Commit 737b940

Browse files
willschlitzerseismanweiji14
authored
Add args_in_kwargs function (#791)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent a618a8d commit 737b940

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

pygmt/helpers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pygmt.helpers.decorators import fmt_docstring, kwargs_to_strings, use_alias
55
from pygmt.helpers.tempfile import GMTTempFile, unique_name
66
from pygmt.helpers.utils import (
7+
args_in_kwargs,
78
build_arg_string,
89
data_kind,
910
dummy_context,

pygmt/helpers/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,29 @@ def launch_external_viewer(fname):
220220
subprocess.run(["open", fname], check=False, **run_args)
221221
else:
222222
webbrowser.open_new_tab("file://{}".format(fname))
223+
224+
225+
def args_in_kwargs(args, kwargs):
226+
"""
227+
Take a list and a dictionary, and determine if any entries in the list are
228+
keys in the dictionary.
229+
230+
This function is used to determine if at least one of the required
231+
arguments is passed to raise a GMTInvalidInput Error.
232+
233+
Parameters
234+
----------
235+
args : list
236+
List of required arguments, using the GMT short-form aliases.
237+
238+
kwargs : dict
239+
The dictionary of kwargs is the format returned by the _preprocess
240+
function of the BasePlotting class. The keys are the GMT
241+
short-form aliases of the parameters.
242+
243+
Returns
244+
--------
245+
bool
246+
If one of the required arguments is in ``kwargs``.
247+
"""
248+
return any(arg in kwargs for arg in args)

pygmt/tests/test_helpers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import numpy as np
77
import pytest
88
from pygmt.exceptions import GMTInvalidInput
9-
from pygmt.helpers import GMTTempFile, data_kind, kwargs_to_strings, unique_name
9+
from pygmt.helpers import (
10+
GMTTempFile,
11+
args_in_kwargs,
12+
data_kind,
13+
kwargs_to_strings,
14+
unique_name,
15+
)
1016

1117

1218
@pytest.mark.parametrize(
@@ -90,3 +96,17 @@ def test_gmttempfile_read():
9096
ftmp.write("in.dat: N = 2\t<1/3>\t<2/4>\n")
9197
assert tmpfile.read() == "in.dat: N = 2 <1/3> <2/4>\n"
9298
assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n"
99+
100+
101+
def test_args_in_kwargs():
102+
"Test that args_in_kwargs function returns correct Boolean responses."
103+
kwargs = {"A": 1, "B": 2, "C": 3}
104+
# Passing list of arguments with passing values in the beginning
105+
passing_args_1 = ["B", "C", "D"]
106+
assert args_in_kwargs(args=passing_args_1, kwargs=kwargs)
107+
# Passing list of arguments that starts with failing arguments
108+
passing_args_2 = ["D", "X", "C"]
109+
assert args_in_kwargs(args=passing_args_2, kwargs=kwargs)
110+
# Failing list of arguments
111+
failing_args = ["D", "E", "F"]
112+
assert not args_in_kwargs(args=failing_args, kwargs=kwargs)

0 commit comments

Comments
 (0)