implement __str__ for all of the proxy stuff so we can

actually print out a statistic.

--HG--
extra : convert_revision : 043be6bd729e74d2220c5ae8aa1fc739aa247715
This commit is contained in:
Nathan Binkert 2005-11-20 23:30:13 -05:00
parent d003c30a8c
commit 67c276ed2e

View file

@ -145,6 +145,8 @@ class ScalarConstant(Scalar):
self.constant = constant self.constant = constant
def __value__(self, run): def __value__(self, run):
return self.constant return self.constant
def __str__(self):
return str(self.constant)
class VectorConstant(Vector): class VectorConstant(Vector):
def __init__(self, constant): def __init__(self, constant):
@ -153,6 +155,8 @@ class VectorConstant(Vector):
return self.constant[index] return self.constant[index]
def __len__(self): def __len__(self):
return len(self.constant) return len(self.constant)
def __str__(self):
return str(self.constant)
def WrapValue(value): def WrapValue(value):
if isinstance(value, (int, long, float)): if isinstance(value, (int, long, float)):
@ -183,6 +187,9 @@ class Statistic(object):
super(Statistic, self).__setattr__(attr, value) super(Statistic, self).__setattr__(attr, value)
def __str__(self):
return self.name
class ValueProxy(Value): class ValueProxy(Value):
def __getattr__(self, attr): def __getattr__(self, attr):
if attr == '__value__': if attr == '__value__':
@ -221,6 +228,14 @@ class UnaryProxy(ValueProxy):
def __vectorlen__(self): def __vectorlen__(self):
return len(unproxy(self.arg)) return len(unproxy(self.arg))
def __str__(self):
if self.op == operator.__neg__:
return '-%s' % str(self.arg)
if self.op == operator.__pos__:
return '+%s' % str(self.arg)
if self.op == operator.__abs__:
return 'abs(%s)' % self.arg
class BinaryProxy(ValueProxy): class BinaryProxy(ValueProxy):
def __init__(self, op, arg0, arg1): def __init__(self, op, arg0, arg1):
super(BinaryProxy, self).__init__() super(BinaryProxy, self).__init__()
@ -271,6 +286,16 @@ class BinaryProxy(ValueProxy):
return len0 return len0
def __str__(self):
ops = { operator.__add__ : '+',
operator.__sub__ : '-',
operator.__mul__ : '*',
operator.__div__ : '/',
operator.__truediv__ : '/',
operator.__floordiv__ : '//' }
return '(%s %s %s)' % (str(self.arg0), ops[self.op], str(self.arg1))
class Proxy(Value): class Proxy(Value):
def __init__(self, name, dict): def __init__(self, name, dict):
self.name = name self.name = name
@ -285,6 +310,9 @@ class Proxy(Value):
def __getattr__(self, attr): def __getattr__(self, attr):
return AttrProxy(self, attr) return AttrProxy(self, attr)
def __str__(self):
return str(self.dict[self.name])
class ItemProxy(Proxy): class ItemProxy(Proxy):
def __init__(self, proxy, index): def __init__(self, proxy, index):
self.proxy = proxy self.proxy = proxy
@ -293,6 +321,9 @@ class ItemProxy(Proxy):
def __unproxy__(self): def __unproxy__(self):
return unproxy(unproxy(self.proxy)[self.index]) return unproxy(unproxy(self.proxy)[self.index])
def __str__(self):
return '%s[%s]' % (self.proxy, self.index)
class AttrProxy(Proxy): class AttrProxy(Proxy):
def __init__(self, proxy, attr): def __init__(self, proxy, attr):
self.proxy = proxy self.proxy = proxy
@ -301,6 +332,9 @@ class AttrProxy(Proxy):
def __unproxy__(self): def __unproxy__(self):
return unproxy(getattr(unproxy(self.proxy), self.attr)) return unproxy(getattr(unproxy(self.proxy), self.attr))
def __str__(self):
return '%s.%s' % (self.proxy, self.attr)
class ProxyGroup(object): class ProxyGroup(object):
def __init__(self, dict=None, **kwargs): def __init__(self, dict=None, **kwargs):
self.__dict__['dict'] = {} self.__dict__['dict'] = {}
@ -362,6 +396,9 @@ class Formula(Value):
value = eval(formula, self.source.stattop) value = eval(formula, self.source.stattop)
return getattr(value, attr) return getattr(value, attr)
def __str__(self):
return self.name
class SimpleDist(Statistic): class SimpleDist(Statistic):
def __init__(self, sums, squares, samples): def __init__(self, sums, squares, samples):
self.sums = sums self.sums = sums