import bitmath class SpecsParser: def __init__(self, exceptional_devices, allowed_unit=10): self.exceptional_devices = exceptional_devices self.allowed_unit = allowed_unit def transform_specs(self, specs): try: for device in filter(lambda x: x not in self.exceptional_devices, specs): parsed = bitmath.parse_string_unsafe(specs[device]) if parsed.base != self.allowed_unit: return False specs[device] = int(parsed.to_Byte()) return True except ValueError as _: return False def get_allowed_units(self): if self.allowed_unit == 10: unit_prefix = bitmath.SI_PREFIXES else: unit_prefix = bitmath.NIST_PREFIXES return list(map(lambda u: u.upper() + "B", unit_prefix))