convert.py revision 2665
172SN/A# Copyright (c) 2005 The Regents of The University of Michigan 21762SN/A# All rights reserved. 372SN/A# 472SN/A# Redistribution and use in source and binary forms, with or without 572SN/A# modification, are permitted provided that the following conditions are 672SN/A# met: redistributions of source code must retain the above copyright 772SN/A# notice, this list of conditions and the following disclaimer; 872SN/A# redistributions in binary form must reproduce the above copyright 972SN/A# notice, this list of conditions and the following disclaimer in the 1072SN/A# documentation and/or other materials provided with the distribution; 1172SN/A# neither the name of the copyright holders nor the names of its 1272SN/A# contributors may be used to endorse or promote products derived from 1372SN/A# this software without specific prior written permission. 1472SN/A# 1572SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1672SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1772SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1872SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1972SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2072SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2172SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2272SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2372SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2472SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2572SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2672SN/A# 272665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert 282665Ssaidi@eecs.umich.edu 2972SN/A# metric prefixes 3072SN/Aexa = 1.0e18 312SN/Apeta = 1.0e15 322SN/Atera = 1.0e12 332SN/Agiga = 1.0e9 342SN/Amega = 1.0e6 352SN/Akilo = 1.0e3 362SN/A 372SN/Amilli = 1.0e-3 382SN/Amicro = 1.0e-6 392SN/Anano = 1.0e-9 402SN/Apico = 1.0e-12 412SN/Afemto = 1.0e-15 422SN/Aatto = 1.0e-18 432SN/A 442SN/A# power of 2 prefixes 452SN/Akibi = 1024 462SN/Amebi = kibi * 1024 472SN/Agibi = mebi * 1024 482SN/Atebi = gibi * 1024 492SN/Apebi = tebi * 1024 502SN/Aexbi = pebi * 1024 512SN/A 522SN/A# memory size configuration stuff 532SN/Adef toFloat(value): 542SN/A if not isinstance(value, str): 552SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 562SN/A 572SN/A if value.endswith('Ei'): 582SN/A return float(value[:-2]) * exbi 592SN/A elif value.endswith('Pi'): 602SN/A return float(value[:-2]) * pebi 612SN/A elif value.endswith('Ti'): 622SN/A return float(value[:-2]) * tebi 632SN/A elif value.endswith('Gi'): 642SN/A return float(value[:-2]) * gibi 652SN/A elif value.endswith('Mi'): 662SN/A return float(value[:-2]) * mebi 672SN/A elif value.endswith('ki'): 682SN/A return float(value[:-2]) * kibi 692SN/A elif value.endswith('E'): 702SN/A return float(value[:-1]) * exa 712SN/A elif value.endswith('P'): 722SN/A return float(value[:-1]) * peta 732SN/A elif value.endswith('T'): 742SN/A return float(value[:-1]) * tera 752SN/A elif value.endswith('G'): 762SN/A return float(value[:-1]) * giga 772SN/A elif value.endswith('M'): 782SN/A return float(value[:-1]) * mega 792SN/A elif value.endswith('k'): 802SN/A return float(value[:-1]) * kilo 812SN/A elif value.endswith('m'): 822SN/A return float(value[:-1]) * milli 832SN/A elif value.endswith('u'): 842SN/A return float(value[:-1]) * micro 852SN/A elif value.endswith('n'): 862SN/A return float(value[:-1]) * nano 872SN/A elif value.endswith('p'): 882SN/A return float(value[:-1]) * pico 892SN/A elif value.endswith('f'): 902SN/A return float(value[:-1]) * femto 912SN/A else: 922SN/A return float(value) 932SN/A 942SN/Adef toInteger(value): 952SN/A value = toFloat(value) 962SN/A result = long(value) 972SN/A if value != result: 982SN/A raise ValueError, "cannot convert '%s' to integer" % value 992SN/A 1002SN/A return result 1012SN/A 1022SN/A_bool_dict = { 1032SN/A 'true' : True, 't' : True, 'yes' : True, 'y' : True, '1' : True, 1042SN/A 'false' : False, 'f' : False, 'no' : False, 'n' : False, '0' : False 1052SN/A } 1062SN/A 1072SN/Adef toBool(value): 1082SN/A if not isinstance(value, str): 1092SN/A raise TypeError, "wrong type '%s' should be str" % type(value) 110