Module refinery.units.meta.iffc

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from refinery.lib.types import INF
from refinery.units.meta import Arg, ConditionalUnit


class iffc(ConditionalUnit, docs='{0}{p}{1}'):
    """
    Filter incoming chunks depending on whether their size is within any of the given bounds.
    """
    def __init__(
        self,
        *bounds: Arg.Bounds(help='Specifies an (inclusive) range to check for.', intok=True),
        retain=False,
    ):
        if not bounds:
            raise ValueError('cannot filter for size without specifying any bounds')
        super().__init__(
            bounds=bounds,
            retain=retain,
        )

    def match(self, chunk):
        length: int = len(chunk)
        for bounds in self.args.bounds:
            if isinstance(bounds, int):
                if length == bounds:
                    return True
            if isinstance(bounds, slice):
                a = bounds.start or 0
                b = bounds.stop or INF
                t = bounds.step or 1
                if a <= length <= b and not (length - a) % t:
                    return True
        return False

Classes

class iffc (*bounds, retain=False)

Filter incoming chunks depending on whether their size is within any of the given bounds.

Note: The reverse operation of a conditional unit uses the logical negation of its condition.

Expand source code Browse git
class iffc(ConditionalUnit, docs='{0}{p}{1}'):
    """
    Filter incoming chunks depending on whether their size is within any of the given bounds.
    """
    def __init__(
        self,
        *bounds: Arg.Bounds(help='Specifies an (inclusive) range to check for.', intok=True),
        retain=False,
    ):
        if not bounds:
            raise ValueError('cannot filter for size without specifying any bounds')
        super().__init__(
            bounds=bounds,
            retain=retain,
        )

    def match(self, chunk):
        length: int = len(chunk)
        for bounds in self.args.bounds:
            if isinstance(bounds, int):
                if length == bounds:
                    return True
            if isinstance(bounds, slice):
                a = bounds.start or 0
                b = bounds.stop or INF
                t = bounds.step or 1
                if a <= length <= b and not (length - a) % t:
                    return True
        return False

Ancestors

Class variables

var required_dependencies
var optional_dependencies

Methods

def match(self, chunk)
Expand source code Browse git
def match(self, chunk):
    length: int = len(chunk)
    for bounds in self.args.bounds:
        if isinstance(bounds, int):
            if length == bounds:
                return True
        if isinstance(bounds, slice):
            a = bounds.start or 0
            b = bounds.stop or INF
            t = bounds.step or 1
            if a <= length <= b and not (length - a) % t:
                return True
    return False

Inherited members