diff options
author | Nathan Binkert <binkertn@umich.edu> | 2005-03-11 18:47:11 -0500 |
---|---|---|
committer | Nathan Binkert <binkertn@umich.edu> | 2005-03-11 18:47:11 -0500 |
commit | c393a51f4a00e88e7a72ad2c7bd56d19194f0b6a (patch) | |
tree | 9cd1da1f592488c5f5d2fdea95725775ee765b61 /python/m5/smartdict.py | |
parent | e6902a907e333c09d64d520f4c978edf7e098f6a (diff) | |
download | gem5-c393a51f4a00e88e7a72ad2c7bd56d19194f0b6a.tar.xz |
move the conversion stuff that was in configs/kernel/Config.py into
the m5 package as convert.py
add a smartdict class which stores strings and can intelligently
interpret those string variables as several other types.
make the env dict use the smartdict class
python/m5/config.py:
move a bunch of conversion functions into convert.py
turn the env dict into a smartdict
adapt the _CheckedInt stuff to deal with derived types
python/m5/objects/BaseCPU.mpy:
env is now a smartdict and can properly convert to bool
--HG--
extra : convert_revision : 8abcd35a5ab14b82f280aea59020953869e33365
Diffstat (limited to 'python/m5/smartdict.py')
-rw-r--r-- | python/m5/smartdict.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/python/m5/smartdict.py b/python/m5/smartdict.py new file mode 100644 index 000000000..e282bc07b --- /dev/null +++ b/python/m5/smartdict.py @@ -0,0 +1,85 @@ +from convert import * + +class SmartDict(dict): + class Proxy(str): + def __int__(self): + return int(to_integer(str(self))) + def __long__(self): + return long(to_integer(str(self))) + def __float__(self): + return float(to_integer(str(self))) + def __nonzero__(self): + return to_bool(str(self)) + def convert(self, other): + t = type(other) + if t == bool: + return bool(self) + if t == int: + return int(self) + if t == long: + return long(self) + if t == float: + return float(self) + return str(self) + def __lt__(self, other): + return self.convert(other) < other + def __le__(self, other): + return self.convert(other) <= other + def __eq__(self, other): + return self.convert(other) == other + def __ne__(self, other): + return self.convert(other) != other + def __gt__(self, other): + return self.convert(other) > other + def __ge__(self, other): + return self.convert(other) >= other + + def __add__(self, other): + return self.convert(other) + other + def __sub__(self, other): + return self.convert(other) - other + def __mul__(self, other): + return self.convert(other) * other + def __div__(self, other): + return self.convert(other) / other + def __truediv__(self, other): + return self.convert(other) / other + + def __radd__(self, other): + return other + self.convert(other) + def __rsub__(self, other): + return other - self.convert(other) + def __rmul__(self, other): + return other * self.convert(other) + def __rdiv__(self, other): + return other / self.convert(other) + def __rtruediv__(self, other): + return other / self.convert(other) + + + def __getitem__(self, key): + return self.Proxy(dict.__getitem__(self, key)) + + def __setitem__(self, key, item): + dict.__setitem__(self, key, str(item)) + + def values(self): + return [ self.Proxy(v) for v in dict.values(self) ] + + def itervalues(self): + for value in dict.itervalues(self): + yield self.Proxy(value) + + def items(self): + return [ (k, self.Proxy(v)) for k,v in dict.items(self) ] + + def iteritems(self): + for key,value in dict.iteritems(self): + yield key, self.Proxy(value) + + def get(self, key, default=''): + return self.Proxy(dict.get(self, key, str(default))) + + def setdefault(self, key, default=''): + return self.Proxy(dict.setdefault(self, key, str(default))) + |