From 087c861df3fd32a0772260225fc1fe87e3adc317 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 16 May 2019 08:15:29 +0200 Subject: utils: Add soft_cast_float; float support in config strings --- lib/utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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): -- cgit v1.2.3