Module refinery.units.blockwise.rev

Expand source code Browse git
from __future__ import annotations

from typing import TYPE_CHECKING

from refinery.units.blockwise import FastBlockError, UnaryOperation

if TYPE_CHECKING:
    from numpy import ndarray


class rev(UnaryOperation):
    """
    By default, this reverses the input string. For different block sizes, the blocks of the
    input data are output in reverse order. If the length of the input data is not a multiple
    of the block size, the data is truncated.
    """

    def __init__(self, blocksize=1):
        super().__init__(blocksize=blocksize, _truncate=2)

    def inplace(self, block: ndarray):
        return self._numpy.flip(block)

    operate = NotImplemented

    def process(self, data: bytearray):
        if self.bytestream:
            data.reverse()
            return data
        try:
            return self._fastblock(data)
        except FastBlockError:
            b = self.blocksize
            n = len(data)
            q = n // b
            m = q * b
            view = memoryview(data)
            temp = bytearray(b)
            for k in range(0, (q // 2) * b, b):
                lhs = slice(k, k + b)
                rhs = slice(m - k - b, m - k)
                temp[:] = view[rhs]
                data[rhs] = view[lhs]
                data[lhs] = temp
            if m < n:
                del view
                del temp
                del data[m:]
            return data

Classes

class rev (blocksize=1)

By default, this reverses the input string. For different block sizes, the blocks of the input data are output in reverse order. If the length of the input data is not a multiple of the block size, the data is truncated.

Expand source code Browse git
class rev(UnaryOperation):
    """
    By default, this reverses the input string. For different block sizes, the blocks of the
    input data are output in reverse order. If the length of the input data is not a multiple
    of the block size, the data is truncated.
    """

    def __init__(self, blocksize=1):
        super().__init__(blocksize=blocksize, _truncate=2)

    def inplace(self, block: ndarray):
        return self._numpy.flip(block)

    operate = NotImplemented

    def process(self, data: bytearray):
        if self.bytestream:
            data.reverse()
            return data
        try:
            return self._fastblock(data)
        except FastBlockError:
            b = self.blocksize
            n = len(data)
            q = n // b
            m = q * b
            view = memoryview(data)
            temp = bytearray(b)
            for k in range(0, (q // 2) * b, b):
                lhs = slice(k, k + b)
                rhs = slice(m - k - b, m - k)
                temp[:] = view[rhs]
                data[rhs] = view[lhs]
                data[lhs] = temp
            if m < n:
                del view
                del temp
                del data[m:]
            return data

Ancestors

Subclasses

Class variables

var operate

The type of the None singleton.

var reverse

The type of the None singleton.

Methods

def inplace(self, block)
Expand source code Browse git
def inplace(self, block: ndarray):
    return self._numpy.flip(block)

Inherited members