From 62919d668c475a9191ff9cc8c4016a3baee1667c Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 8 May 2019 16:30:47 +0200 Subject: add cpu cycles -> cpu energy and packet size -> tx energy for protocolmodeling --- lib/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib/utils.py') 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. -- cgit v1.2.3