diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2019-05-16 08:15:29 +0200 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2019-05-16 08:15:29 +0200 |
commit | 087c861df3fd32a0772260225fc1fe87e3adc317 (patch) | |
tree | c81ac4c77732a4b697885efa96b1532adf77cc51 /lib | |
parent | 3e47f0d359afb3b0f73bc54eea8ace81acd3caa2 (diff) |
utils: Add soft_cast_float; float support in config strings
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/utils.py b/lib/utils.py index b06e2eb..effbd46 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -45,6 +45,20 @@ def soft_cast_int(n): except ValueError: return n +def soft_cast_float(n): + """ + Convert to float, 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 float(n) + except ValueError: + return n + def flatten(somelist): """ @@ -58,7 +72,7 @@ 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) + conf_dict[key] = soft_cast_float(value) return conf_dict def param_slice_eq(a, b, index): |