-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvalue_normalizer.py
57 lines (36 loc) · 1.1 KB
/
value_normalizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import logging
import re
def isstring(value): return isinstance(value, str)
def isfloat(value: str): return re.match(r'^-?\d+(?:\.\d+)?$', value) is not None
def isinteger(value: str): return re.match(r'^\d+$', value) is not None
def isvalidmetric(value): return isinstance(value, int) or isinstance(value, float) or isinstance(value, bool)
type_mapping = {
"yes": 1,
"no": 0,
"up": 1,
"down": 0,
True: 1,
False: 0,
"MOUNTED": 1,
"UNMOUNTED": 0
}
def normalize_value(value):
if value is None:
return None
value = type_mapping.get(value) if type_mapping.get(value) is not None else value
if isstring(value):
value = parse_string(value)
if isvalidmetric(value):
return value
else:
logging.warning("Value: " + str(value) + " is not valid metric type")
return None
def parse_string(value):
value = remove_data_unit(value)
if isinteger(value):
value = int(value)
elif isfloat(value):
value = float(value)
return value
def remove_data_unit(value: str):
return value.replace(" kB", "")