当前位置: 首页>>代码示例>>Python>>正文


Python array.reverse方法代码示例

本文整理汇总了Python中array.array.reverse方法的典型用法代码示例。如果您正苦于以下问题:Python array.reverse方法的具体用法?Python array.reverse怎么用?Python array.reverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在array.array的用法示例。


在下文中一共展示了array.reverse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: tobytes

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import reverse [as 别名]
 def tobytes(self, msb=False, msby=False):
     """Convert the sequence into a sequence of byte values"""
     blength = (len(self)+7) & (~0x7)
     sequence = list(self._seq)
     if not msb:
         sequence.reverse()
     bytes_ = Array('B')
     for pos in xrange(0, blength, 8):
         seq = sequence[pos:pos+8]
         byte = 0
         while seq:
             byte <<= 1
             byte |= seq.pop(0)
         bytes_.append(byte)
     if msby:
         bytes_.reverse()
     return bytes_.tolist()
开发者ID:adamfeuer,项目名称:pyftdi,代码行数:19,代码来源:bits.py

示例2: BitSequence

# 需要导入模块: from array import array [as 别名]
# 或者: from array.array import reverse [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
#.........这里部分代码省略.........
开发者ID:adamfeuer,项目名称:pyftdi,代码行数:103,代码来源:bits.py


注:本文中的array.array.reverse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。