summaryrefslogtreecommitdiff
path: root/lib/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils.py')
-rw-r--r--lib/utils.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/utils.py b/lib/utils.py
index 64f1780..02c1d26 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -31,6 +31,21 @@ def float_or_nan(n):
except ValueError:
return np.nan
+def soft_cast_int(n):
+ """
+ Convert to int, if possible.
+
+ If it is empty, returns None.
+ If it is not numeric, it is left unchanged.
+ """
+ if n == None or n == '':
+ return None
+ try:
+ return int(n)
+ except ValueError:
+ return n
+
+
def flatten(somelist):
"""
Flatten a list.
@@ -39,6 +54,13 @@ def flatten(somelist):
"""
return [item for sublist in somelist for item in sublist]
+def parse_conf_str(conf_str):
+ conf_dict = dict()
+ for option in conf_str.split(','):
+ key, value = option.split(':')
+ conf_dict[key] = soft_cast_int(value)
+ return conf_dict
+
def param_slice_eq(a, b, index):
"""
Check if by_param keys a and b are identical, ignoring the parameter at index.