def test_operator(self):
import operator
self.assertIs(operator.truth(0), False)
self.assertIs(operator.truth(1), True)
with test_support.check_py3k_warnings():
self.assertIs(operator.isCallable(0), False)
self.assertIs(operator.isCallable(len), True)
self.assertIs(operator.isNumberType(None), False)
self.assertIs(operator.isNumberType(0), True)
self.assertIs(operator.not_(1), False)
self.assertIs(operator.not_(0), True)
self.assertIs(operator.isSequenceType(0), False)
self.assertIs(operator.isSequenceType([]), True)
self.assertIs(operator.contains([], 1), False)
self.assertIs(operator.contains([1], 1), True)
self.assertIs(operator.isMappingType(1), False)
self.assertIs(operator.isMappingType({}), True)
self.assertIs(operator.lt(0, 0), False)
self.assertIs(operator.lt(0, 1), True)
self.assertIs(operator.is_(True, True), True)
self.assertIs(operator.is_(True, False), False)
self.assertIs(operator.is_not(True, True), False)
self.assertIs(operator.is_not(True, False), True)
python类isSequenceType()的实例源码
def test_operator(self):
import operator
self.assertIs(operator.truth(0), False)
self.assertIs(operator.truth(1), True)
with test_support.check_py3k_warnings():
self.assertIs(operator.isCallable(0), False)
self.assertIs(operator.isCallable(len), True)
self.assertIs(operator.isNumberType(None), False)
self.assertIs(operator.isNumberType(0), True)
self.assertIs(operator.not_(1), False)
self.assertIs(operator.not_(0), True)
self.assertIs(operator.isSequenceType(0), False)
self.assertIs(operator.isSequenceType([]), True)
self.assertIs(operator.contains([], 1), False)
self.assertIs(operator.contains([1], 1), True)
self.assertIs(operator.isMappingType(1), False)
self.assertIs(operator.isMappingType({}), True)
self.assertIs(operator.lt(0, 0), False)
self.assertIs(operator.lt(0, 1), True)
self.assertIs(operator.is_(True, True), True)
self.assertIs(operator.is_(True, False), False)
self.assertIs(operator.is_not(True, True), False)
self.assertIs(operator.is_not(True, False), True)
def test_operator(self):
import operator
self.assertIs(operator.truth(0), False)
self.assertIs(operator.truth(1), True)
with test_support.check_py3k_warnings():
self.assertIs(operator.isCallable(0), False)
self.assertIs(operator.isCallable(len), True)
self.assertIs(operator.isNumberType(None), False)
self.assertIs(operator.isNumberType(0), True)
self.assertIs(operator.not_(1), False)
self.assertIs(operator.not_(0), True)
self.assertIs(operator.isSequenceType(0), False)
self.assertIs(operator.isSequenceType([]), True)
self.assertIs(operator.contains([], 1), False)
self.assertIs(operator.contains([1], 1), True)
self.assertIs(operator.isMappingType(1), False)
self.assertIs(operator.isMappingType({}), True)
self.assertIs(operator.lt(0, 0), False)
self.assertIs(operator.lt(0, 1), True)
self.assertIs(operator.is_(True, True), True)
self.assertIs(operator.is_(True, False), False)
self.assertIs(operator.is_not(True, True), False)
self.assertIs(operator.is_not(True, False), True)
def test_operator(self):
import operator
self.assertIs(operator.truth(0), False)
self.assertIs(operator.truth(1), True)
with test_support.check_py3k_warnings():
self.assertIs(operator.isCallable(0), False)
self.assertIs(operator.isCallable(len), True)
self.assertIs(operator.isNumberType(None), False)
self.assertIs(operator.isNumberType(0), True)
self.assertIs(operator.not_(1), False)
self.assertIs(operator.not_(0), True)
self.assertIs(operator.isSequenceType(0), False)
self.assertIs(operator.isSequenceType([]), True)
self.assertIs(operator.contains([], 1), False)
self.assertIs(operator.contains([1], 1), True)
self.assertIs(operator.isMappingType(1), False)
self.assertIs(operator.isMappingType({}), True)
self.assertIs(operator.lt(0, 0), False)
self.assertIs(operator.lt(0, 1), True)
self.assertIs(operator.is_(True, True), True)
self.assertIs(operator.is_(True, False), False)
self.assertIs(operator.is_not(True, True), False)
self.assertIs(operator.is_not(True, False), True)
def __init__(self, stream, cols=None, number_fmt='%8.4f', col_width=8,
col_sep=' ', output_block=False, **pars):
DataRecorder.__init__(self, **pars)
self._stream = stream
if not number_fmt.startswith('%'):
number_fmt = '%%s' % number_fmt
self._number_fmt = number_fmt
self._col_sep = col_sep
self._col_width = col_width
if operator.isSequenceType(cols) and \
not isinstance(cols, (str, unicode)):
cols = CaselessList(cols)
elif operator.isNumberType(cols):
cols = cols
else:
cols = None
self._columns = cols
self._output_block = output_block
def setEnvObj(self, obj):
"""Sets the environment for the given object. If object is a sequence
then each pair of elements k, v is added as env[k] = v.
If object is a map then the environmnent is updated.
Other object types are not supported
The elements which are strings are 'python evaluated'
@throws TypeError is obj is not a sequence or a map
@param[in] obj object to be added to the environment
@return a dict representing the added environment"""
if operator.isSequenceType(obj) and \
not isinstance(obj, (str, unicode)):
obj = self._dictFromSequence(obj)
elif not operator.isMappingType(obj):
raise TypeError("obj parameter must be a sequence or a map")
obj = self._encode(obj)
for k, v in obj.iteritems():
self._setOneEnv(k, v)
return obj
def __init__(self, allowedMethods, *args):
Exception.__init__(self, allowedMethods, *args)
self.allowedMethods = allowedMethods
if not operator.isSequenceType(allowedMethods):
why = "but my first argument is not a sequence."
s = ("First argument must be a sequence of"
" supported methods, %s" % (why,))
raise TypeError, s
def is_sequence(obj):
"""
Helper function to determine sequences
across Python 2.x and 3.x
"""
try:
from collections import Sequence
except ImportError:
from operator import isSequenceType
return isSequenceType(obj)
else:
return isinstance(obj, Sequence)
def __init__(self,*args):
assert args
if len(args)==1:
if isinstance(args[0],basestring):
args = args[0].split('.')
elif isSequenceType(args[0]):
args = args[0]
else:
args = [args]
self._tuple = tuple(args)
def __init__(self,arg=None):
""" Initializes the list with a sequence or an initial value. """
if arg is None:
list.__init__(self)
elif operator.isSequenceType(arg):
list.__init__(self,arg)
else:
list.__init__(self)
self.append(arg,1)
def test_isSequenceType(self):
self.assertRaises(TypeError, operator.isSequenceType)
self.assertTrue(operator.isSequenceType(dir()))
self.assertTrue(operator.isSequenceType(()))
self.assertTrue(operator.isSequenceType(xrange(10)))
self.assertTrue(operator.isSequenceType('yeahbuddy'))
self.assertFalse(operator.isSequenceType(3))
class Dict(dict): pass
self.assertFalse(operator.isSequenceType(Dict()))
def test_isSequenceType(self):
self.assertRaises(TypeError, operator.isSequenceType)
self.assertTrue(operator.isSequenceType(dir()))
self.assertTrue(operator.isSequenceType(()))
self.assertTrue(operator.isSequenceType(xrange(10)))
self.assertTrue(operator.isSequenceType('yeahbuddy'))
self.assertFalse(operator.isSequenceType(3))
class Dict(dict): pass
self.assertFalse(operator.isSequenceType(Dict()))
def __init__(self, allowedMethods, *args):
Exception.__init__(self, allowedMethods, *args)
self.allowedMethods = allowedMethods
if not operator.isSequenceType(allowedMethods):
why = "but my first argument is not a sequence."
s = ("First argument must be a sequence of"
" supported methods, %s" % (why,))
raise TypeError, s
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
cpp_message.py 文件源码
项目:bigmuddy-network-telemetry-collector
作者: cisco
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
def is_sequence(other):
return operator.isSequenceType(other)
def test_isSequenceType(self):
self.assertRaises(TypeError, operator.isSequenceType)
self.assertTrue(operator.isSequenceType(dir()))
self.assertTrue(operator.isSequenceType(()))
self.assertTrue(operator.isSequenceType(xrange(10)))
self.assertTrue(operator.isSequenceType('yeahbuddy'))
self.assertFalse(operator.isSequenceType(3))
class Dict(dict): pass
self.assertFalse(operator.isSequenceType(Dict()))
def test_isSequenceType(self):
self.assertRaises(TypeError, operator.isSequenceType)
self.assertTrue(operator.isSequenceType(dir()))
self.assertTrue(operator.isSequenceType(()))
self.assertTrue(operator.isSequenceType(xrange(10)))
self.assertTrue(operator.isSequenceType('yeahbuddy'))
self.assertFalse(operator.isSequenceType(3))
class Dict(dict): pass
self.assertFalse(operator.isSequenceType(Dict()))
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
def get_plot(self):
self.plot = ''
if hasattr(self.page, 'EditorialReviews'):
if hasattr(self.page.EditorialReviews, 'EditorialReview'):
if isSequenceType(self.page.EditorialReviews.EditorialReview):
for review in self.page.EditorialReviews.EditorialReview:
if hasattr(review, 'Source') and \
hasattr(review, 'Content') and \
string.find(review.Source, 'Amazon') > -1:
self.plot = review.Content
else:
if hasattr(self.page.EditorialReviews.EditorialReview, 'Source') and \
hasattr(self.page.EditorialReviews.EditorialReview, 'Content') and \
string.find(self.page.EditorialReviews.EditorialReview.Source, 'Amazon') > -1:
self.plot = self.page.EditorialReviews.EditorialReview.Content
def get_genre(self):
# BrowseNodeId 547664 (Genres)
self.genre = ''
delimiter = ''
if hasattr(self.page, 'BrowseNodes') and hasattr(self.page.BrowseNodes, 'BrowseNode'):
if isSequenceType(self.page.BrowseNodes.BrowseNode):
for node in self.page.BrowseNodes.BrowseNode:
parentnode = node
while hasattr(parentnode, 'Ancestors') and parentnode.BrowseNodeId <> '547664' \
and parentnode.BrowseNodeId <> '13628901': # no production countries; they are also arranged under genres
parentnode = parentnode.Ancestors.BrowseNode
if parentnode.BrowseNodeId == '547664':
self.genre = self.genre + delimiter + node.Name
delimiter = ', '
def get_country(self):
# BrowseNodeId 13628901 (production countries)
self.country = ''
delimiter = ''
if hasattr(self.page, 'BrowseNodes') and hasattr(self.page.BrowseNodes, 'BrowseNode'):
if isSequenceType(self.page.BrowseNodes.BrowseNode):
for node in self.page.BrowseNodes.BrowseNode:
parentnode = node
while hasattr(parentnode, 'Ancestors') and parentnode.BrowseNodeId <> '13628901':
parentnode = parentnode.Ancestors.BrowseNode
if parentnode.BrowseNodeId == '13628901':
self.country = self.country + delimiter + node.Name
delimiter = ', '
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
def _convert_to_svm_node_array(x):
""" convert a sequence or mapping to an svm_node array """
import operator
# Find non zero elements
iter_range = []
if type(x) == dict:
for k, v in x.iteritems():
# all zeros kept due to the precomputed kernel; no good solution yet
# if v != 0:
iter_range.append(k)
elif operator.isSequenceType(x):
for j in range(len(x)):
# if x[j] != 0:
iter_range.append(j)
else:
raise TypeError, "data must be a mapping or a sequence"
iter_range.sort()
data = svmc.svm_node_array(len(iter_range) + 1)
svmc.svm_node_array_set(data, len(iter_range), -1, 0)
j = 0
for k in iter_range:
svmc.svm_node_array_set(data, j, k, x[k])
j = j + 1
return data
def point(self, lut, mode=None):
"Map image through lookup table"
self.load()
if isinstance(lut, ImagePointHandler):
return lut.point(self)
if not isSequenceType(lut):
# if it isn't a list, it should be a function
if self.mode in ("I", "I;16", "F"):
# check if the function can be used with point_transform
scale, offset = _getscaleoffset(lut)
return self._new(self.im.point_transform(scale, offset))
# for other modes, convert the function to a table
lut = map(lut, range(256)) * self.im.bands
if self.mode == "F":
# FIXME: _imaging returns a confusing error message for this case
raise ValueError("point operation not supported for this mode")
return self._new(self.im.point(lut, mode))
##
# Adds or replaces the alpha layer in this image. If the image
# does not have an alpha layer, it's converted to "LA" or "RGBA".
# The new layer must be either "L" or "1".
#
# @param im The new alpha layer. This can either be an "L" or "1"
# image having the same size as this image, or an integer or
# other color value.
def __eq__(self, other):
if self is other:
return True
if not operator.isSequenceType(other):
raise TypeError(
'Can only compare repeated scalar fields against sequences.')
# We are presumably comparing against some other sequence type.
return other == self[slice(None, None, None)]
def point(self, lut, mode=None):
"Map image through lookup table"
self.load()
if isinstance(lut, ImagePointHandler):
return lut.point(self)
if not isSequenceType(lut):
# if it isn't a list, it should be a function
if self.mode in ("I", "I;16", "F"):
# check if the function can be used with point_transform
scale, offset = _getscaleoffset(lut)
return self._new(self.im.point_transform(scale, offset))
# for other modes, convert the function to a table
lut = map(lut, range(256)) * self.im.bands
if self.mode == "F":
# FIXME: _imaging returns a confusing error message for this case
raise ValueError("point operation not supported for this mode")
return self._new(self.im.point(lut, mode))
##
# Adds or replaces the alpha layer in this image. If the image
# does not have an alpha layer, it's converted to "LA" or "RGBA".
# The new layer must be either "L" or "1".
#
# @param im The new alpha layer. This can either be an "L" or "1"
# image having the same size as this image, or an integer or
# other color value.
def point(self, lut, mode=None):
"Map image through lookup table"
self.load()
if isinstance(lut, ImagePointHandler):
return lut.point(self)
if not isSequenceType(lut):
# if it isn't a list, it should be a function
if self.mode in ("I", "I;16", "F"):
# check if the function can be used with point_transform
scale, offset = _getscaleoffset(lut)
return self._new(self.im.point_transform(scale, offset))
# for other modes, convert the function to a table
lut = map(lut, range(256)) * self.im.bands
if self.mode == "F":
# FIXME: _imaging returns a confusing error message for this case
raise ValueError("point operation not supported for this mode")
return self._new(self.im.point(lut, mode))
##
# Adds or replaces the alpha layer in this image. If the image
# does not have an alpha layer, it's converted to "LA" or "RGBA".
# The new layer must be either "L" or "1".
#
# @param im The new alpha layer. This can either be an "L" or "1"
# image having the same size as this image, or an integer or
# other color value.