Skip to content

Commit ef2bce6

Browse files
args tests
1 parent 5fb5f71 commit ef2bce6

2 files changed

Lines changed: 99 additions & 69 deletions

File tree

cs_util/args.py

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def parse_options(p_def, short_options, types, help_strings, args=None):
2424
command line options types
2525
help_strings : dict
2626
command line options help strings
27+
args : list, optional
28+
list of arguments, default is None, in which case sys.args[1:]
29+
is used
2730
2831
Returns
2932
-------
@@ -84,78 +87,88 @@ def parse_options(p_def, short_options, types, help_strings, args=None):
8487
help=f"verbose output",
8588
)
8689

87-
options, my_args = parser.parse_args(args)
90+
options_values, my_args = parser.parse_args(args)
91+
92+
# Transform parameter values to dict
93+
options = {}
94+
for key in vars(options_values):
95+
options[key] = getattr(options_values, key)
96+
97+
# Add other default values which do not have argument option
98+
for key in p_def:
99+
if key not in options:
100+
options[key] = p_def[key]
88101

89102
return options
90103

91104

92-
def my_string_split(string, num=-1, verbose=False, stop=False, sep=None):
93-
"""My String Split.
94-
95-
Split a *string* into a list of strings. Choose as separator
96-
the first in the list [space, underscore] that occurs in the string.
97-
(Thus, if both occur, use space.)
98-
99-
Parameters
100-
----------
101-
string : str
102-
Input string
103-
num : int
104-
Required length of output list of strings, -1 if no requirement.
105-
verbose : bool
106-
Verbose output
107-
stop : bool
108-
Stop programs with error if True, return None and continues otherwise
109-
sep : bool
110-
Separator, try ' ', '_', and '.' if None (default)
111-
112-
Raises
113-
------
114-
ValueError
115-
If number of elements in string and num are different, for stop=True
116-
If no separator found in string
117-
118-
Returns
119-
-------
120-
list
121-
List of string on success, and None if failed
122-
123-
"""
124-
if string is None:
125-
return None
126-
127-
if sep is None:
128-
has_space = string.find(" ")
129-
has_underscore = string.find("_")
130-
has_dot = string.find(".")
131-
132-
if has_space != -1:
133-
my_sep = " "
134-
elif has_underscore != -1:
135-
my_sep = "_"
136-
elif has_dot != -1:
137-
my_sep = "."
138-
else:
105+
def my_string_split(string, num=-1, verbose=False, stop=False, sep=None):
106+
"""My String Split.
107+
108+
Split a *string* into a list of strings. Choose as separator
109+
the first in the list [space, underscore] that occurs in the string.
110+
(Thus, if both occur, use space.)
111+
112+
Parameters
113+
----------
114+
string : str
115+
Input string
116+
num : int
117+
Required length of output list of strings, -1 if no requirement.
118+
verbose : bool
119+
Verbose output
120+
stop : bool
121+
Stop programs with error if True, return None and continues otherwise
122+
sep : bool
123+
Separator, try ' ', '_', and '.' if None (default)
124+
125+
Raises
126+
------
127+
ValueError
128+
If number of elements in string and num are different, for stop=True
129+
If no separator found in string
130+
131+
Returns
132+
-------
133+
list
134+
List of string on success, and None if failed
135+
136+
"""
137+
if string is None:
138+
return None
139+
140+
if sep is None:
141+
has_space = string.find(" ")
142+
has_underscore = string.find("_")
143+
has_dot = string.find(".")
144+
145+
if has_space != -1:
146+
my_sep = " "
147+
elif has_underscore != -1:
148+
my_sep = "_"
149+
elif has_dot != -1:
150+
my_sep = "."
151+
else:
139152
# no separator found, does string consist of only one element?
140-
if num == -1 or num == 1:
141-
my_sep = None
142-
else:
143-
raise ValueError(
144-
"No separator (' ', '_', or '.') found in string"
145-
+ f" '{string}', cannot split"
146-
)
147-
else:
148-
if not string.find(sep):
149-
raise ValueError(
153+
if num == -1 or num == 1:
154+
my_sep = None
155+
else:
156+
raise ValueError(
157+
"No separator (' ', '_', or '.') found in string"
158+
+ f" '{string}', cannot split"
159+
)
160+
else:
161+
if not string.find(sep):
162+
raise ValueError(
150163
f"No separator '{sep}' found in string '{string}' cannot split"
151-
)
152-
my_sep = sep
153-
154-
res = string.split(my_sep)
155-
156-
if num != -1 and num != len(res) and stop:
164+
)
165+
my_sep = sep
166+
167+
res = string.split(my_sep)
168+
169+
if num != -1 and num != len(res) and stop:
157170
raise ValueError(
158171
f"String '{len(res)}' has length {num}, required is {num}"
159172
)
160-
173+
161174
return res

cs_util/tests/test_args.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import numpy as np
1010
from numpy import testing as npt
11+
import optparse
12+
import pytest
1113

1214
from unittest import TestCase
1315

@@ -22,27 +24,31 @@ def setUp(self):
2224
self._params_def = {
2325
"p_int": 1,
2426
"p_float": 0.612,
25-
"p_bool": True,
2627
"p_str": "some_string",
28+
"p_bool": -1,
29+
"p_Bool": -1,
2730
"p_str_def": "second_string",
2831
"p_none": "",
2932
}
3033
self._types = {
3134
"p_int": "int",
3235
"p_float": "float",
3336
"p_bool": "bool",
37+
"p_Bool": "bool",
3438
"p_str": "str",
3539
}
3640
self._short_options = {
3741
"p_int": "-i",
3842
"p_float": "-f",
3943
"p_bool": "-b",
44+
"p_Bool": "-B",
4045
"p_str": "-s",
4146
}
4247
self._help_strings = {
4348
"p_int": "integer option, default={}",
4449
"p_float": "float option, default={}",
45-
"p_bool": "bool option, default={}",
50+
"p_bool": "bool option, set to True if given",
51+
"p_Bool": "bool option, set to True if given",
4652
"p_str": "string option, default={}",
4753
}
4854

@@ -60,6 +66,17 @@ def test_parse_options(self):
6066
self._short_options,
6167
self._types,
6268
self._help_strings,
63-
args=["-i", "2", "-s", "test"],
69+
args=["-i", "2", "-s", "test", "-b"],
6470
)
65-
print(self._options)
71+
72+
# Test updated options
73+
npt.assert_equal(self._options["p_int"], 2)
74+
npt.assert_equal(self._options["p_str"], "test")
75+
npt.assert_equal(self._options["p_bool"], True)
76+
77+
# Test unchanged (default) options
78+
npt.assert_equal(self._options["p_float"], 0.612)
79+
npt.assert_equal(self._options["p_Bool"], False)
80+
npt.assert_equal(self._options["p_str_def"], "second_string")
81+
82+
# Test exceptions TBD

0 commit comments

Comments
 (0)