Skip to content

Commit 69a18b8

Browse files
author
martinkilbinger
committed
Added function to read python parameter script
1 parent f519868 commit 69a18b8

1 file changed

Lines changed: 56 additions & 14 deletions

File tree

cs_util/args.py

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"""
99

1010
import sys
11+
import os
12+
1113
from optparse import OptionParser
1214

1315

@@ -45,18 +47,10 @@ def parse_options(p_def, short_options, types, help_strings, args=None):
4547
# Process if help string exists
4648
if key in help_strings:
4749
# Set short option
48-
if key in short_options:
49-
short = short_options[key]
50-
else:
51-
# Default is no short option
52-
short = ""
50+
short = short_options[key] if key in short_options else ""
5351

5452
# Set type
55-
if key in types:
56-
typ = types[key]
57-
else:
58-
# Default is str
59-
typ = "string"
53+
typ = types[key] if key in types else "string"
6054

6155
# Special case: bool type
6256
if typ == "bool":
@@ -84,15 +78,15 @@ def parse_options(p_def, short_options, types, help_strings, args=None):
8478
"--verbose",
8579
dest="verbose",
8680
action="store_true",
87-
help=f"verbose output",
81+
help="verbose output",
8882
)
8983

9084
options_values, my_args = parser.parse_args(args)
9185

9286
# Transform parameter values to dict
93-
options = {}
94-
for key in vars(options_values):
95-
options[key] = getattr(options_values, key)
87+
options = {
88+
key: getattr(options_values, key) for key in vars(options_values)
89+
}
9690

9791
# Add other default values which do not have argument option
9892
for key in p_def:
@@ -172,3 +166,51 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None):
172166
)
173167

174168
return res
169+
170+
171+
def read_param_script(params_in_path, params_def, verbose=False):
172+
"""Read Params Scritpt.
173+
174+
Reads a python parameter script, or prompts user for input if file does not
175+
exist.
176+
Returns updated parameters.
177+
178+
Parameters
179+
----------
180+
params_in_path : str
181+
input file path
182+
params : dict
183+
default parameters
184+
verbose : bool, optional
185+
verbose output if ``True``; default is ``False``
186+
187+
Returns
188+
-------
189+
dict :
190+
updated parameters
191+
192+
"""
193+
params_in = {}
194+
params_out = {}
195+
for key in params_def:
196+
params_out[key] = params_def[key]
197+
198+
if os.path.exists(params_in_path):
199+
if verbose:
200+
print(f"Reading parameter script {params_in_path}")
201+
with open(params_in_path) as f:
202+
exec(f.read())
203+
# Set instance parameters, copy from above
204+
for key in params_in:
205+
params_out[key] = params_in[key]
206+
else:
207+
print(
208+
f"Configuration script {params_in_path} not found, asking for user input"
209+
)
210+
for key in params_def:
211+
msg = f"{key}? [{params_def[key]}] "
212+
val_user = input(msg)
213+
if val_user != "":
214+
params_out[key] = val_user
215+
216+
return params_out

0 commit comments

Comments
 (0)