get rid of issequence and just use the isinstance builtin
--HG-- extra : convert_revision : eca99aa35ad5c5c1c86325f55cf693ff585c9826
This commit is contained in:
parent
0acb2d0108
commit
c2014cb5ad
4 changed files with 32 additions and 36 deletions
|
@ -35,9 +35,6 @@ try:
|
||||||
except:
|
except:
|
||||||
noDot = True
|
noDot = True
|
||||||
|
|
||||||
def issequence(value):
|
|
||||||
return isinstance(value, tuple) or isinstance(value, list)
|
|
||||||
|
|
||||||
class Singleton(type):
|
class Singleton(type):
|
||||||
def __call__(cls, *args, **kwargs):
|
def __call__(cls, *args, **kwargs):
|
||||||
if hasattr(cls, '_instance'):
|
if hasattr(cls, '_instance'):
|
||||||
|
@ -181,7 +178,7 @@ def isSimObject(value):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def isSimObjSequence(value):
|
def isSimObjSequence(value):
|
||||||
if not issequence(value):
|
if not isinstance(value, (list, tuple)):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for val in value:
|
for val in value:
|
||||||
|
@ -455,7 +452,7 @@ class MetaConfigNode(type):
|
||||||
if isNullPointer(child) or instance.top_child_names.has_key(name):
|
if isNullPointer(child) or instance.top_child_names.has_key(name):
|
||||||
return
|
return
|
||||||
|
|
||||||
if issequence(child):
|
if isinstance(child, (list, tuple)):
|
||||||
kid = []
|
kid = []
|
||||||
for i,c in enumerate(child):
|
for i,c in enumerate(child):
|
||||||
n = '%s%d' % (name, i)
|
n = '%s%d' % (name, i)
|
||||||
|
@ -483,10 +480,10 @@ class MetaConfigNode(type):
|
||||||
for key,value in cls._getvalues().iteritems():
|
for key,value in cls._getvalues().iteritems():
|
||||||
if isConfigNode(value):
|
if isConfigNode(value):
|
||||||
cls.add_child(instance, key, value)
|
cls.add_child(instance, key, value)
|
||||||
if issequence(value):
|
if isinstance(value, (list, tuple)):
|
||||||
list = [ v for v in value if isConfigNode(v) ]
|
vals = [ v for v in value if isConfigNode(v) ]
|
||||||
if len(list):
|
if len(vals):
|
||||||
cls.add_child(instance, key, list)
|
cls.add_child(instance, key, vals)
|
||||||
|
|
||||||
for pname,param in cls._getparams().iteritems():
|
for pname,param in cls._getparams().iteritems():
|
||||||
try:
|
try:
|
||||||
|
@ -497,7 +494,7 @@ class MetaConfigNode(type):
|
||||||
try:
|
try:
|
||||||
if isConfigNode(value):
|
if isConfigNode(value):
|
||||||
value = instance.child_objects[value]
|
value = instance.child_objects[value]
|
||||||
elif issequence(value):
|
elif isinstance(value, (list, tuple)):
|
||||||
v = []
|
v = []
|
||||||
for val in value:
|
for val in value:
|
||||||
if isConfigNode(val):
|
if isConfigNode(val):
|
||||||
|
@ -699,7 +696,7 @@ class Node(object):
|
||||||
pval = param.value
|
pval = param.value
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if issequence(pval):
|
if isinstance(pval, (list, tuple)):
|
||||||
param.value = [ self.unproxy(ptype, pv) for pv in pval ]
|
param.value = [ self.unproxy(ptype, pv) for pv in pval ]
|
||||||
else:
|
else:
|
||||||
param.value = self.unproxy(ptype, pval)
|
param.value = self.unproxy(ptype, pval)
|
||||||
|
@ -952,7 +949,7 @@ class _VectorParam(_Param):
|
||||||
if value == None:
|
if value == None:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if issequence(value):
|
if isinstance(value, (list, tuple)):
|
||||||
for val in value:
|
for val in value:
|
||||||
if not isinstance(val, Proxy):
|
if not isinstance(val, Proxy):
|
||||||
self.ptype._convert(val)
|
self.ptype._convert(val)
|
||||||
|
@ -965,7 +962,7 @@ class _VectorParam(_Param):
|
||||||
if value == None:
|
if value == None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if issequence(value):
|
if isinstance(value, (list, tuple)):
|
||||||
# list: coerce each element into new list
|
# list: coerce each element into new list
|
||||||
return [ self.ptype._convert(v) for v in value ]
|
return [ self.ptype._convert(v) for v in value ]
|
||||||
else:
|
else:
|
||||||
|
@ -973,7 +970,7 @@ class _VectorParam(_Param):
|
||||||
return self.ptype._convert(value)
|
return self.ptype._convert(value)
|
||||||
|
|
||||||
def string(self, value):
|
def string(self, value):
|
||||||
if issequence(value):
|
if isinstance(value, (list, tuple)):
|
||||||
return ' '.join([ self.ptype._string(v) for v in value])
|
return ' '.join([ self.ptype._string(v) for v in value])
|
||||||
else:
|
else:
|
||||||
return self.ptype._string(value)
|
return self.ptype._string(value)
|
||||||
|
@ -1339,8 +1336,7 @@ class SimObject(ConfigNode, ParamType):
|
||||||
# __all__ defines the list of symbols that get exported when
|
# __all__ defines the list of symbols that get exported when
|
||||||
# 'from config import *' is invoked. Try to keep this reasonably
|
# 'from config import *' is invoked. Try to keep this reasonably
|
||||||
# short to avoid polluting other namespaces.
|
# short to avoid polluting other namespaces.
|
||||||
__all__ = ['issequence',
|
__all__ = ['ConfigNode', 'SimObject', 'ParamContext', 'Param', 'VectorParam',
|
||||||
'ConfigNode', 'SimObject', 'ParamContext', 'Param', 'VectorParam',
|
|
||||||
'Super', 'Enum',
|
'Super', 'Enum',
|
||||||
'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
|
'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
|
||||||
'Int32', 'UInt32', 'Int64', 'UInt64',
|
'Int32', 'UInt32', 'Int64', 'UInt64',
|
||||||
|
|
|
@ -68,7 +68,7 @@ class VectorDisplay:
|
||||||
p.flags = self.flags
|
p.flags = self.flags
|
||||||
p.precision = self.precision
|
p.precision = self.precision
|
||||||
|
|
||||||
if issequence(self.value):
|
if isinstance(self.value, (list, tuple)):
|
||||||
if not len(self.value):
|
if not len(self.value):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,6 @@ display_run = 0
|
||||||
global globalTicks
|
global globalTicks
|
||||||
globalTicks = None
|
globalTicks = None
|
||||||
|
|
||||||
def issequence(t):
|
|
||||||
return isinstance(t, types.TupleType) or isinstance(t, types.ListType)
|
|
||||||
|
|
||||||
def total(f):
|
def total(f):
|
||||||
if isinstance(f, FormulaStat):
|
if isinstance(f, FormulaStat):
|
||||||
v = f.value
|
v = f.value
|
||||||
|
@ -16,7 +13,7 @@ def total(f):
|
||||||
v = f
|
v = f
|
||||||
|
|
||||||
f = FormulaStat()
|
f = FormulaStat()
|
||||||
if issequence(v):
|
if isinstance(v, (list, tuple)):
|
||||||
f.value = reduce(operator.add, v)
|
f.value = reduce(operator.add, v)
|
||||||
else:
|
else:
|
||||||
f.value = v
|
f.value = v
|
||||||
|
@ -29,7 +26,7 @@ def unaryop(op, f):
|
||||||
else:
|
else:
|
||||||
v = f
|
v = f
|
||||||
|
|
||||||
if issequence(v):
|
if isinstance(v, (list, tuple)):
|
||||||
return map(op, v)
|
return map(op, v)
|
||||||
else:
|
else:
|
||||||
return op(v)
|
return op(v)
|
||||||
|
@ -109,19 +106,19 @@ def binaryop(op, lf, rf):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def sums(x, y):
|
def sums(x, y):
|
||||||
if issequence(x):
|
if isinstance(x, (list, tuple)):
|
||||||
return map(lambda x, y: x + y, x, y)
|
return map(lambda x, y: x + y, x, y)
|
||||||
else:
|
else:
|
||||||
return x + y
|
return x + y
|
||||||
|
|
||||||
def alltrue(list):
|
def alltrue(seq):
|
||||||
return reduce(lambda x, y: x and y, list)
|
return reduce(lambda x, y: x and y, seq)
|
||||||
|
|
||||||
def allfalse(list):
|
def allfalse(seq):
|
||||||
return not reduce(lambda x, y: x or y, list)
|
return not reduce(lambda x, y: x or y, seq)
|
||||||
|
|
||||||
def enumerate(list):
|
def enumerate(seq):
|
||||||
return map(None, range(len(list)), list)
|
return map(None, range(len(seq)), seq)
|
||||||
|
|
||||||
def cmp(a, b):
|
def cmp(a, b):
|
||||||
if a < b:
|
if a < b:
|
||||||
|
@ -323,10 +320,11 @@ class Vector(Statistic,FormulaStat):
|
||||||
len(self.value) == len(other.value)
|
len(self.value) == len(other.value)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if issequence(self.value) != issequence(other.value):
|
if isinstance(self.value, (list, tuple)) != \
|
||||||
|
isinstance(other.value, (list, tuple)):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if issequence(self.value):
|
if isinstance(self.value, (list, tuple)):
|
||||||
if len(self.value) != len(other.value):
|
if len(self.value) != len(other.value):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
|
@ -348,7 +346,7 @@ class Vector(Statistic,FormulaStat):
|
||||||
def __itruediv__(self, other):
|
def __itruediv__(self, other):
|
||||||
if not other:
|
if not other:
|
||||||
return self
|
return self
|
||||||
if issequence(self.value):
|
if isinstance(self.value, (list, tuple)):
|
||||||
for i in xrange(len(self.value)):
|
for i in xrange(len(self.value)):
|
||||||
self.value[i] /= other
|
self.value[i] /= other
|
||||||
else:
|
else:
|
||||||
|
@ -642,7 +640,8 @@ class VectorDist(Statistic):
|
||||||
return alltrue(map(lambda x, y : x == y, self.dist, other.dist))
|
return alltrue(map(lambda x, y : x == y, self.dist, other.dist))
|
||||||
|
|
||||||
def __isub__(self, other):
|
def __isub__(self, other):
|
||||||
if issequence(self.dist) and issequence(other.dist):
|
if isinstance(self.dist, (list, tuple)) and \
|
||||||
|
isinstance(other.dist, (list, tuple)):
|
||||||
for sd,od in zip(self.dist, other.dist):
|
for sd,od in zip(self.dist, other.dist):
|
||||||
sd -= od
|
sd -= od
|
||||||
else:
|
else:
|
||||||
|
@ -650,7 +649,8 @@ class VectorDist(Statistic):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __iadd__(self, other):
|
def __iadd__(self, other):
|
||||||
if issequence(self.dist) and issequence(other.dist):
|
if isinstance(self.dist, (list, tuple)) and \
|
||||||
|
isinstance(other.dist, (list, tuple)):
|
||||||
for sd,od in zip(self.dist, other.dist):
|
for sd,od in zip(self.dist, other.dist):
|
||||||
sd += od
|
sd += od
|
||||||
else:
|
else:
|
||||||
|
@ -660,7 +660,7 @@ class VectorDist(Statistic):
|
||||||
def __itruediv__(self, other):
|
def __itruediv__(self, other):
|
||||||
if not other:
|
if not other:
|
||||||
return self
|
return self
|
||||||
if issequence(self.dist):
|
if isinstance(self.dist, (list, tuple)):
|
||||||
for dist in self.dist:
|
for dist in self.dist:
|
||||||
dist /= other
|
dist /= other
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -71,7 +71,7 @@ class VectorDisplay:
|
||||||
p.flags = self.flags
|
p.flags = self.flags
|
||||||
p.precision = self.precision
|
p.precision = self.precision
|
||||||
|
|
||||||
if issequence(self.value):
|
if isinstance(self.value, (list, tuple)):
|
||||||
if not len(self.value):
|
if not len(self.value):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue