convert.py revision 1736
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 toLong(value): 93 value = toFloat(value) 94 result = int(value) 95 if value != result: 96 raise ValueError, "cannot convert '%s' to long" % value 97 98 return result 99 100def toInteger(value): 101 value = toFloat(value) 102 result = int(value) 103 if value != result: 104 raise ValueError, "cannot convert '%s' to integer" % value 105 106 return result 107 108def toBool(value): 109 if not isinstance(value, str): 110 raise TypeError, "wrong type '%s' should be str" % type(value) 111 112 value = value.lower() 113 if value == "true" or value == "t" or value == "yes" or value == "y": 114 return True 115 elif value == "false" or value == "f" or value == "no" or value == "n": 116 return False 117 118 raise ValueError, "cannot convert '%s' to bool" % value 119 120def toFrequency(value): 121 if not isinstance(value, str): 122 raise TypeError, "wrong type '%s' should be str" % type(value) 123 124 if value.endswith('THz'): 125 return float(value[:-3]) * tera 126 elif value.endswith('GHz'): 127 return float(value[:-3]) * giga 128 elif value.endswith('MHz'): 129 return float(value[:-3]) * mega 130 elif value.endswith('kHz'): 131 return float(value[:-3]) * kilo 132 elif value.endswith('Hz'): 133 return float(value[:-2]) 134 135 raise ValueError, "cannot convert '%s' to frequency" % value 136 137def toLatency(value): 138 if not isinstance(value, str): 139 raise TypeError, "wrong type '%s' should be str" % type(value) 140 141 if value.endswith('ps'): 142 return float(value[:-2]) * pico 143 elif value.endswith('ns'): 144 return float(value[:-2]) * nano 145 elif value.endswith('us'): 146 return float(value[:-2]) * micro 147 elif value.endswith('ms'): 148 return float(value[:-2]) * milli 149 elif value.endswith('s'): 150 return float(value[:-1]) 151 152 raise ValueError, "cannot convert '%s' to latency" % value 153 154def toClockPeriod(value): 155 """result is a clock period""" 156 157 if not isinstance(value, str): 158 raise TypeError, "wrong type '%s' should be str" % type(value) 159 160 try: 161 val = toFrequency(value) 162 if val != 0: 163 val = 1 / val 164 return val 165 except ValueError: 166 pass 167 168 try: 169 val = toLatency(value) 170 return val 171 except ValueError: 172 pass 173 174 raise ValueError, "cannot convert '%s' to clock period" % value 175 176 177def toNetworkBandwidth(value): 178 if not isinstance(value, str): 179 raise TypeError, "wrong type '%s' should be str" % type(value) 180 181 if value.endswith('Tbps'): 182 return float(value[:-4]) * tera 183 elif value.endswith('Gbps'): 184 return float(value[:-4]) * giga 185 elif value.endswith('Mbps'): 186 return float(value[:-4]) * mega 187 elif value.endswith('kbps'): 188 return float(value[:-4]) * kilo 189 elif value.endswith('bps'): 190 return float(value[:-3]) 191 else: 192 return float(value) 193 194 raise ValueError, "cannot convert '%s' to network bandwidth" % value 195 196def toMemoryBandwidth(value): 197 if not isinstance(value, str): 198 raise TypeError, "wrong type '%s' should be str" % type(value) 199 200 if value.endswith('PB/s'): 201 return float(value[:-4]) * pebi 202 elif value.endswith('TB/s'): 203 return float(value[:-4]) * tebi 204 elif value.endswith('GB/s'): 205 return float(value[:-4]) * gibi 206 elif value.endswith('MB/s'): 207 return float(value[:-4]) * mebi 208 elif value.endswith('kB/s'): 209 return float(value[:-4]) * kibi 210 elif value.endswith('B/s'): 211 return float(value[:-3]) 212 213 raise ValueError, "cannot convert '%s' to memory bandwidth" % value 214 215def toMemorySize(value): 216 if not isinstance(value, str): 217 raise TypeError, "wrong type '%s' should be str" % type(value) 218 219 if value.endswith('PB'): 220 return float(value[:-2]) * pebi 221 elif value.endswith('TB'): 222 return float(value[:-2]) * tebi 223 elif value.endswith('GB'): 224 return float(value[:-2]) * gibi 225 elif value.endswith('MB'): 226 return float(value[:-2]) * mebi 227 elif value.endswith('kB'): 228 return float(value[:-2]) * kibi 229 elif value.endswith('B'): 230 return float(value[:-1]) 231 232 raise ValueError, "cannot convert '%s' to memory size" % value 233