Add value_normalizer.py

This commit is contained in:
Vitaliy Skrypnyk
2020-07-27 21:40:39 +03:00
committed by Vitaliy Skrypnyk
parent b56a2064b9
commit 98c1682f6e
2 changed files with 47 additions and 16 deletions

35
value_normalizer.py Normal file
View File

@@ -0,0 +1,35 @@
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('^\d+$', value) is not None)
def isvalidmetric(value) : return isinstance(value, int) or isinstance(value, float) or isinstance(value, bool)
def normalize_data(data):
if isinstance(data, dict):
for key in data:
value = data.get(key)
if isstring(value):
value = remove_data_unit(value)
if isinteger(value):
data[key] = int(value)
continue
if isfloat(value):
data[key] = float(value)
continue
return data
def remove_data_unit(value: str):
return value.replace(" kB", "")