2005-03-12 00:47:11 +01:00
|
|
|
# metric prefixes
|
|
|
|
exa = 1.0e18
|
|
|
|
peta = 1.0e15
|
|
|
|
tera = 1.0e12
|
|
|
|
giga = 1.0e9
|
|
|
|
mega = 1.0e6
|
|
|
|
kilo = 1.0e3
|
|
|
|
|
|
|
|
milli = 1.0e-3
|
|
|
|
micro = 1.0e-6
|
|
|
|
nano = 1.0e-9
|
|
|
|
pico = 1.0e-12
|
|
|
|
femto = 1.0e-15
|
|
|
|
atto = 1.0e-18
|
|
|
|
|
|
|
|
# power of 2 prefixes
|
|
|
|
kibi = 1024
|
|
|
|
mebi = kibi * 1024
|
|
|
|
gibi = mebi * 1024
|
|
|
|
tebi = gibi * 1024
|
|
|
|
pebi = tebi * 1024
|
|
|
|
exbi = pebi * 1024
|
|
|
|
|
|
|
|
# memory size configuration stuff
|
2005-03-26 04:59:29 +01:00
|
|
|
def toFloat(value):
|
2005-03-12 00:47:11 +01:00
|
|
|
if not isinstance(value, str):
|
2005-03-26 04:59:29 +01:00
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
if value.endswith('Ei'):
|
|
|
|
return float(value[:-2]) * exbi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Pi'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * pebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Ti'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * tebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Gi'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * gibi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Mi'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * mebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('ki'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * kibi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('E'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * exa
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('P'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * peta
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('T'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * tera
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('G'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * giga
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('M'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * mega
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('k'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * kilo
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('m'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * milli
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('u'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * micro
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('n'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * nano
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('p'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * pico
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('f'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1]) * femto
|
2005-03-12 00:47:11 +01:00
|
|
|
else:
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value)
|
|
|
|
|
|
|
|
def toLong(value):
|
|
|
|
value = toFloat(value)
|
|
|
|
result = int(value)
|
|
|
|
if value != result:
|
|
|
|
raise ValueError, "cannot convert '%s' to long" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
def toInteger(value):
|
|
|
|
value = toFloat(value)
|
|
|
|
result = int(value)
|
|
|
|
if value != result:
|
|
|
|
raise ValueError, "cannot convert '%s' to integer" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
return result
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
def toBool(value):
|
|
|
|
if not isinstance(value, str):
|
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
value = value.lower()
|
|
|
|
if value == "true" or value == "t" or value == "yes" or value == "y":
|
|
|
|
return True
|
|
|
|
elif value == "false" or value == "f" or value == "no" or value == "n":
|
|
|
|
return False
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
raise ValueError, "cannot convert '%s' to bool" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-22 20:51:31 +01:00
|
|
|
def toFrequency(value):
|
2005-03-12 00:47:11 +01:00
|
|
|
if not isinstance(value, str):
|
2005-03-26 04:59:29 +01:00
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
if value.endswith('THz'):
|
|
|
|
return float(value[:-3]) * tera
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('GHz'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-3]) * giga
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('MHz'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-3]) * mega
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('kHz'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-3]) * kilo
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Hz'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2])
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
raise ValueError, "cannot convert '%s' to frequency" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-22 20:51:31 +01:00
|
|
|
def toLatency(value):
|
2005-03-12 00:47:11 +01:00
|
|
|
if not isinstance(value, str):
|
2005-03-26 04:59:29 +01:00
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
if value.endswith('ps'):
|
|
|
|
return float(value[:-2]) * pico
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('ns'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * nano
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('us'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * micro
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('ms'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * milli
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('s'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1])
|
|
|
|
|
|
|
|
raise ValueError, "cannot convert '%s' to latency" % value
|
|
|
|
|
|
|
|
def toClockPeriod(value):
|
|
|
|
"""result is a clock period"""
|
|
|
|
|
|
|
|
if not isinstance(value, str):
|
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
try:
|
|
|
|
val = toFrequency(value)
|
|
|
|
if val != 0:
|
|
|
|
val = 1 / val
|
|
|
|
return val
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
val = toLatency(value)
|
|
|
|
return val
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
raise ValueError, "cannot convert '%s' to clock period" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
|
|
|
|
2005-03-22 20:51:31 +01:00
|
|
|
def toNetworkBandwidth(value):
|
2005-03-12 00:47:11 +01:00
|
|
|
if not isinstance(value, str):
|
2005-03-26 04:59:29 +01:00
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
if value.endswith('Tbps'):
|
2005-04-06 23:59:31 +02:00
|
|
|
return float(value[:-4]) * tera
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Gbps'):
|
2005-04-06 23:59:31 +02:00
|
|
|
return float(value[:-4]) * giga
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('Mbps'):
|
2005-04-06 23:59:31 +02:00
|
|
|
return float(value[:-4]) * mega
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('kbps'):
|
2005-04-06 23:59:31 +02:00
|
|
|
return float(value[:-4]) * kilo
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('bps'):
|
2005-04-06 23:59:31 +02:00
|
|
|
return float(value[:-3])
|
2005-03-12 00:47:11 +01:00
|
|
|
else:
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value)
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
raise ValueError, "cannot convert '%s' to network bandwidth" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-22 20:51:31 +01:00
|
|
|
def toMemoryBandwidth(value):
|
2005-03-12 00:47:11 +01:00
|
|
|
if not isinstance(value, str):
|
2005-03-26 04:59:29 +01:00
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
if value.endswith('PB/s'):
|
|
|
|
return float(value[:-4]) * pebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('TB/s'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-4]) * tebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('GB/s'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-4]) * gibi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('MB/s'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-4]) * mebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('kB/s'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-4]) * kibi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('B/s'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-3])
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
raise ValueError, "cannot convert '%s' to memory bandwidth" % value
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-22 20:51:31 +01:00
|
|
|
def toMemorySize(value):
|
2005-03-12 00:47:11 +01:00
|
|
|
if not isinstance(value, str):
|
2005-03-26 04:59:29 +01:00
|
|
|
raise TypeError, "wrong type '%s' should be str" % type(value)
|
|
|
|
|
|
|
|
if value.endswith('PB'):
|
|
|
|
return float(value[:-2]) * pebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('TB'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * tebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('GB'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * gibi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('MB'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * mebi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('kB'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-2]) * kibi
|
2005-03-12 00:47:11 +01:00
|
|
|
elif value.endswith('B'):
|
2005-03-26 04:59:29 +01:00
|
|
|
return float(value[:-1])
|
2005-03-12 00:47:11 +01:00
|
|
|
|
2005-03-26 04:59:29 +01:00
|
|
|
raise ValueError, "cannot convert '%s' to memory size" % value
|