convert.py revision 1627
1# metric prefixes 2exa = 1.0e18 3peta = 1.0e15 4tera = 1.0e12 5giga = 1.0e9 6mega = 1.0e6 7kilo = 1.0e3 8 9milli = 1.0e-3 10micro = 1.0e-6 11nano = 1.0e-9 12pico = 1.0e-12 13femto = 1.0e-15 14atto = 1.0e-18 15 16# power of 2 prefixes 17kibi = 1024 18mebi = kibi * 1024 19gibi = mebi * 1024 20tebi = gibi * 1024 21pebi = tebi * 1024 22exbi = pebi * 1024 23 24# memory size configuration stuff 25def toFloat(value): 26 if not isinstance(value, str): 27 raise TypeError, "wrong type '%s' should be str" % type(value) 28 29 if value.endswith('Ei'): 30 return float(value[:-2]) * exbi 31 elif value.endswith('Pi'): 32 return float(value[:-2]) * pebi 33 elif value.endswith('Ti'): 34 return float(value[:-2]) * tebi 35 elif value.endswith('Gi'): 36 return float(value[:-2]) * gibi 37 elif value.endswith('Mi'): 38 return float(value[:-2]) * mebi 39 elif value.endswith('ki'): 40 return float(value[:-2]) * kibi 41 elif value.endswith('E'): 42 return float(value[:-1]) * exa 43 elif value.endswith('P'): 44 return float(value[:-1]) * peta 45 elif value.endswith('T'): 46 return float(value[:-1]) * tera 47 elif value.endswith('G'): 48 return float(value[:-1]) * giga 49 elif value.endswith('M'): 50 return float(value[:-1]) * mega 51 elif value.endswith('k'): 52 return float(value[:-1]) * kilo 53 elif value.endswith('m'): 54 return float(value[:-1]) * milli 55 elif value.endswith('u'): 56 return float(value[:-1]) * micro 57 elif value.endswith('n'): 58 return float(value[:-1]) * nano 59 elif value.endswith('p'): 60 return float(value[:-1]) * pico 61 elif value.endswith('f'): 62 return float(value[:-1]) * femto 63 else: 64 return float(value) 65 66def toLong(value): 67 value = toFloat(value) 68 result = int(value) 69 if value != result: 70 raise ValueError, "cannot convert '%s' to long" % value 71 72 return result 73 74def toInteger(value): 75 value = toFloat(value) 76 result = int(value) 77 if value != result: 78 raise ValueError, "cannot convert '%s' to integer" % value 79 80 return result 81 82def toBool(value): 83 if not isinstance(value, str): 84 raise TypeError, "wrong type '%s' should be str" % type(value) 85 86 value = value.lower() 87 if value == "true" or value == "t" or value == "yes" or value == "y": 88 return True 89 elif value == "false" or value == "f" or value == "no" or value == "n": 90 return False 91 92 raise ValueError, "cannot convert '%s' to bool" % value 93 94def toFrequency(value): 95 if not isinstance(value, str): 96 raise TypeError, "wrong type '%s' should be str" % type(value) 97 98 if value.endswith('THz'): 99 return float(value[:-3]) * tera 100 elif value.endswith('GHz'): 101 return float(value[:-3]) * giga 102 elif value.endswith('MHz'): 103 return float(value[:-3]) * mega 104 elif value.endswith('kHz'): 105 return float(value[:-3]) * kilo 106 elif value.endswith('Hz'): 107 return float(value[:-2]) 108 109 raise ValueError, "cannot convert '%s' to frequency" % value 110 111def toLatency(value): 112 if not isinstance(value, str): 113 raise TypeError, "wrong type '%s' should be str" % type(value) 114 115 if value.endswith('ps'): 116 return float(value[:-2]) * pico 117 elif value.endswith('ns'): 118 return float(value[:-2]) * nano 119 elif value.endswith('us'): 120 return float(value[:-2]) * micro 121 elif value.endswith('ms'): 122 return float(value[:-2]) * milli 123 elif value.endswith('s'): 124 return float(value[:-1]) 125 126 raise ValueError, "cannot convert '%s' to latency" % value 127 128def toClockPeriod(value): 129 """result is a clock period""" 130 131 if not isinstance(value, str): 132 raise TypeError, "wrong type '%s' should be str" % type(value) 133 134 try: 135 val = toFrequency(value) 136 if val != 0: 137 val = 1 / val 138 return val 139 except ValueError: 140 pass 141 142 try: 143 val = toLatency(value) 144 return val 145 except ValueError: 146 pass 147 148 raise ValueError, "cannot convert '%s' to clock period" % value 149 150 151def toNetworkBandwidth(value): 152 if not isinstance(value, str): 153 raise TypeError, "wrong type '%s' should be str" % type(value) 154 155 if value.endswith('Tbps'): 156 return float(value[:-4]) * tera 157 elif value.endswith('Gbps'): 158 return float(value[:-4]) * giga 159 elif value.endswith('Mbps'): 160 return float(value[:-4]) * mega 161 elif value.endswith('kbps'): 162 return float(value[:-4]) * kilo 163 elif value.endswith('bps'): 164 return float(value[:-3]) 165 else: 166 return float(value) 167 168 raise ValueError, "cannot convert '%s' to network bandwidth" % value 169 170def toMemoryBandwidth(value): 171 if not isinstance(value, str): 172 raise TypeError, "wrong type '%s' should be str" % type(value) 173 174 if value.endswith('PB/s'): 175 return float(value[:-4]) * pebi 176 elif value.endswith('TB/s'): 177 return float(value[:-4]) * tebi 178 elif value.endswith('GB/s'): 179 return float(value[:-4]) * gibi 180 elif value.endswith('MB/s'): 181 return float(value[:-4]) * mebi 182 elif value.endswith('kB/s'): 183 return float(value[:-4]) * kibi 184 elif value.endswith('B/s'): 185 return float(value[:-3]) 186 187 raise ValueError, "cannot convert '%s' to memory bandwidth" % value 188 189def toMemorySize(value): 190 if not isinstance(value, str): 191 raise TypeError, "wrong type '%s' should be str" % type(value) 192 193 if value.endswith('PB'): 194 return float(value[:-2]) * pebi 195 elif value.endswith('TB'): 196 return float(value[:-2]) * tebi 197 elif value.endswith('GB'): 198 return float(value[:-2]) * gibi 199 elif value.endswith('MB'): 200 return float(value[:-2]) * mebi 201 elif value.endswith('kB'): 202 return float(value[:-2]) * kibi 203 elif value.endswith('B'): 204 return float(value[:-1]) 205 206 raise ValueError, "cannot convert '%s' to memory size" % value 207