本文整理汇总了Python中array.array.__iter__方法的典型用法代码示例。如果您正苦于以下问题:Python array.__iter__方法的具体用法?Python array.__iter__怎么用?Python array.__iter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类array.array
的用法示例。
在下文中一共展示了array.__iter__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BitSequence
# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import __iter__ [as 别名]
class BitSequence(object):
"""Bit sequence.
Support most of the common bit operations: or, and, shift, comparison,
and conversion from and to integral values.
Bit sequence objects are iterable.
Can be initialized with another bit sequence, a integral value,
a sequence of bytes or an iterable of common boolean values.
:param value: initial value
:param msb: most significant bit first or not
:param length: count of signficant bits in the bit sequence
:param bytes_: initial value specified as a sequence of bytes
:param msby: most significant byte first or not
"""
__slots__ = [ '_seq' ]
def __init__(self, value=None, msb=False, length=0, bytes_=None, msby=True):
"""Instanciate a new bit sequence.
"""
self._seq = Array('B')
seq = self._seq
if value and bytes_:
raise BitSequenceError("Cannot inialize with both a value and "
"bytes")
if bytes_:
provider = msby and list(bytes_).__iter__() or reversed(bytes_)
for byte in provider:
if isinstance(byte, str):
byte = ord(byte)
elif byte > 0xff:
raise BitSequenceError("Invalid byte value")
b = []
for x in xrange(8):
b.append(byte&0x1 and True or False)
byte >>= 1
if msb:
b.reverse()
seq.extend(b)
else:
value = self._tomutable(value)
if isinstance(value, Integral):
self._init_from_integer(value, msb, length)
elif isinstance(value, BitSequence):
self._init_from_sibling(value, msb)
elif is_iterable(value):
self._init_from_iterable(value, msb)
elif value is None:
pass
else:
raise BitSequenceError("Cannot initialize from a %s" % type(value))
self._update_length(length, msb)
def sequence(self):
"""Return the internal representation as a new mutable sequence"""
return Array('B', self._seq)
def reverse(self):
"""In-place reverse"""
self._seq.reverse()
return self
def invert(self):
"""In-place invert sequence values"""
self._seq = Array('B', [x^1 for x in self._seq])
return self
def append(self, seq):
"""Concatenate a new BitSequence"""
if not isinstance(seq, BitSequence):
seq = BitSequence(seq)
self._seq.extend(seq.sequence())
return self
def lsr(self, count):
"Left shift rotate"
count %= len(self)
self._seq[:] = self._seq[count:] + self._seq[:count]
def rsr(self, count):
"Right shift rotate"
count %= len(self)
self._seq[:] = self._seq[-count:] + self._seq[:-count]
def tobit(self):
"""Degenerate the sequence into a single bit, if possible"""
if len(self) != 1:
raise BitSequenceError("BitSequence should be a scalar")
return self._seq[0] and True or False
def tobyte(self, msb=False):
"""Convert the sequence into a single byte value, if possible"""
if len(self) > 8:
raise BitSequenceError("Cannot fit into a single byte")
byte = 0
pos = not msb and -1 or 0
# copy the sequence
#.........这里部分代码省略.........