convert.py revision 1944
1# Copyright (c) 2005 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the 10# documentation and/or other materials provided with the distribution; 11# neither the name of the copyright holders nor the names of its 12# contributors may be used to endorse or promote products derived from 13# this software without specific prior written permission. 14# 15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27# metric prefixes 28exa = 1.0e18 29peta = 1.0e15 30tera = 1.0e12 31giga = 1.0e9 32mega = 1.0e6 33kilo = 1.0e3 34 35milli = 1.0e-3 36micro = 1.0e-6 37nano = 1.0e-9 38pico = 1.0e-12 39femto = 1.0e-15 40atto = 1.0e-18 41 42# power of 2 prefixes 43kibi = 1024 44mebi = kibi * 1024 45gibi = mebi * 1024 46tebi = gibi * 1024 47pebi = tebi * 1024 48exbi = pebi * 1024 49 50# memory size configuration stuff 51def toFloat(value): 52 if not isinstance(value, str): 53 raise TypeError, "wrong type '%s' should be str" % type(value) 54 55 if value.endswith('Ei'): 56 return float(value[:-2]) * exbi 57 elif value.endswith('Pi'): 58 return float(value[:-2]) * pebi 59 elif value.endswith('Ti'): 60 return float(value[:-2]) * tebi 61 elif value.endswith('Gi'): 62 return float(value[:-2]) * gibi 63 elif value.endswith('Mi'): 64 return float(value[:-2]) * mebi 65 elif value.endswith('ki'): 66 return float(value[:-2]) * kibi 67 elif value.endswith('E'): 68 return float(value[:-1]) * exa 69 elif value.endswith('P'): 70 return float(value[:-1]) * peta 71 elif value.endswith('T'): 72 return float(value[:-1]) * tera 73 elif value.endswith('G'): 74 return float(value[:-1]) * giga 75 elif value.endswith('M'): 76 return float(value[:-1]) * mega 77 elif value.endswith('k'): 78 return float(value[:-1]) * kilo 79 elif value.endswith('m'): 80 return float(value[:-1]) * milli 81 elif value.endswith('u'): 82 return float(value[:-1]) * micro 83 elif value.endswith('n'): 84 return float(value[:-1]) * nano 85 elif value.endswith('p'): 86 return float(value[:-1]) * pico 87 elif value.endswith('f'): 88 return float(value[:-1]) * femto 89 else: 90 return float(value) 91 92def toInteger(value): 93 value = toFloat(value) 94 result = long(value) 95 if value != result: 96 raise ValueError, "cannot convert '%s' to integer" % value 97 98 return result 99 100_bool_dict = { 101 'true' : True, 't' : True, 'yes' : True, 'y' : True, '1' : True, 102 'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False 103 } 104 105def toBool(value): 106 if not isinstance(value, str): 107 raise TypeError, "wrong type '%s' should be str" % type(value) 108 109 value = value.lower() 110 result = _bool_dict.get(value, None) 111 if result == None: 112 raise ValueError, "cannot convert '%s' to bool" % value 113 return result 114 115def toFrequency(value): 116 if not isinstance(value, str): 117 raise TypeError, "wrong type '%s' should be str" % type(value) 118 119 if value.endswith('THz'): 120 return float(value[:-3]) * tera 121 elif value.endswith('GHz'): 122 return float(value[:-3]) * giga 123 elif value.endswith('MHz'): 124 return float(value[:-3]) * mega 125 elif value.endswith('kHz'): 126 return float(value[:-3]) * kilo 127 elif value.endswith('Hz'): 128 return float(value[:-2]) 129 130 raise ValueError, "cannot convert '%s' to frequency" % value 131 132def toLatency(value): 133 if not isinstance(value, str): 134 raise TypeError, "wrong type '%s' should be str" % type(value) 135 136 if value.endswith('ps'): 137 return float(value[:-2]) * pico 138 elif value.endswith('ns'): 139 return float(value[:-2]) * nano 140 elif value.endswith('us'): 141 return float(value[:-2]) * micro 142 elif value.endswith('ms'): 143 return float(value[:-2]) * milli 144 elif value.endswith('s'): 145 return float(value[:-1]) 146 147 raise ValueError, "cannot convert '%s' to latency" % value 148 149def toClockPeriod(value): 150 """result is a clock period""" 151 152 if not isinstance(value, str): 153 raise TypeError, "wrong type '%s' should be str" % type(value) 154 155 try: 156 val = toFrequency(value) 157 if val != 0: 158 val = 1 / val 159 return val 160 except ValueError: 161 pass 162 163 try: 164 val = toLatency(value) 165 return val 166 except ValueError: 167 pass 168 169 raise ValueError, "cannot convert '%s' to clock period" % value 170 171 172def toNetworkBandwidth(value): 173 if not isinstance(value, str): 174 raise TypeError, "wrong type '%s' should be str" % type(value) 175 176 if value.endswith('Tbps'): 177 return float(value[:-4]) * tera 178 elif value.endswith('Gbps'): 179 return float(value[:-4]) * giga 180 elif value.endswith('Mbps'): 181 return float(value[:-4]) * mega 182 elif value.endswith('kbps'): 183 return float(value[:-4]) * kilo 184 elif value.endswith('bps'): 185 return float(value[:-3]) 186 else: 187 return float(value) 188 189 raise ValueError, "cannot convert '%s' to network bandwidth" % value 190 191def toMemoryBandwidth(value): 192 if not isinstance(value, str): 193 raise TypeError, "wrong type '%s' should be str" % type(value) 194 195 if value.endswith('PB/s'): 196 return float(value[:-4]) * pebi 197 elif value.endswith('TB/s'): 198 return float(value[:-4]) * tebi 199 elif value.endswith('GB/s'): 200 return float(value[:-4]) * gibi 201 elif value.endswith('MB/s'): 202 return float(value[:-4]) * mebi 203 elif value.endswith('kB/s'): 204 return float(value[:-4]) * kibi 205 elif value.endswith('B/s'): 206 return float(value[:-3]) 207 208 raise ValueError, "cannot convert '%s' to memory bandwidth" % value 209 210def toMemorySize(value): 211 if not isinstance(value, str): 212 raise TypeError, "wrong type '%s' should be str" % type(value) 213 214 if value.endswith('PB'): 215 return long(value[:-2]) * pebi 216 elif value.endswith('TB'): 217 return long(value[:-2]) * tebi 218 elif value.endswith('GB'): 219 return long(value[:-2]) * gibi 220 elif value.endswith('MB'): 221 return long(value[:-2]) * mebi 222 elif value.endswith('kB'): 223 return long(value[:-2]) * kibi 224 elif value.endswith('B'): 225 return long(value[:-1]) 226 227 raise ValueError, "cannot convert '%s' to memory size" % value 228