Skip to main content

Let you register and use filters in right way

Project description

filterutil

filterutil is simple, yet very powerful filtering tool.

How to install

You could install from PyPi:

$ python3 -m pip install filterutil

How to use

Simple filter

from filterutil import Filter


def test_func():
    my_filter = Filter(lambda x: x == 1)
    # assert False
    assert not my_filter.apply(2):

Filter with saved args and kwargs

from typing import Set
from filterutil import Filter


def filtering_func(value: int, y: int):
    return value == y


def test_func():
    # saved args works
    my_filter = Filter(filtering_func, 1)
    # but we recommend to use kwargs
    my_filter = Filter(filtering_func, y=1)

    # assert False
    assert not my_filter.apply(2):
    # .apply under the hood works as
    assert not filtering_func(2, y=1)

    # assert True
    assert my_filter.apply(1):

Coupling filters with AND logic gate

True only if all inputs are True.

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 1)
    b = Filter(lambda x: isinstance(x, int))
    compound_filter = a & b
    # order matters:
    #     "compound_filter = a & b" means first a then b
    #     "compound_filter = b & a" means first b then a
    
    # assert False
    assert not compound_filter.apply(2)

Coupling filters with OR logic gate

True if at least one input is True.

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 1)
    b = Filter(lambda x: isinstance(x, int))
    # order still matters
    compound_filter = a | b

    # assert True
    assert compound_filter.apply(2)

Coupling filters with XOR logic gate

True if exactly one input is True.

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 2)
    b = Filter(lambda x: isintance(x, int))
    compound_filter = a ^ b

    # assert True
    assert compound_filter.apply(1)
    # assert False
    assert not compound_filter.apply(2)

Coupling filters with XNOR logic gate

True if both inputs are the same.

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 2)
    b = Filter(lambda x: isintance(x, str))
    compound_filter = a.xnor(b)

    # assert True
    assert compound_filter.apply(1)

Coupling filters with NOR logic gate

True only if all inputs are False.

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 2)
    b = Filter(lambda x: isintance(x, str))
    compound_filter = a.nor(b)

    # assert True
    assert compound_filter.apply(1)

Coupling filters with NAND logic gate

False only if all inputs are True.

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 1)
    b = Filter(lambda x: isintance(x, int))
    compound_filter = a.nand(b)

    # assert False
    assert not compound_filter.apply(1)

Infinite nesting

from filterutil import Filter


def test_func():
    a = Filter(lambda x: isinstance(x, int))
    b = Filter(lambda x: isinstance(x, str))
    c = Filter(lambda x: x == 1)
    # multiline
    compound_filter = a | b
    compound_filter = compound_filter & c
    # order is: (a or b) and c
    # assert False
    assert not compound_filter.apply(2)

    # is not the same as inline
    # because of python operator precedence
    compound_filter = filter_a | filter_b & filter_c
    # order is: a or (b and c)
    # assert True
    assert compound_filter.apply(2)

Collection of filters with same logic gate

from filterutil import (
    Filter,
    Filters,
    OrFilters,
    XorFilters,
    XnorFilters,
    NandFilters,
    NorFilters,
    LogicGate,
)


def test_func():
    # Filters is AND collection by default
    and_filters = Filters()
    and_filters['a'] = Filter(lambda x: isinstance(x, int))
    and_filters['b'] = Filter(lambda x: isinstance(x, str))
    
    # same as
    and_filters = Filters(
        a=Filter(lambda x: isinstance(x, int)),
        b=Filter(lambda x: isinstance(x, str)),
    )
    # ---

    # assert False
    assert not and_filters.apply(2)

    # OR collection
    or_filters = OrFilters()
    
    #same as 
    or_filters = Filters(LogicGate.OR)
    # ---

    or_filters['a'] = Filter(lambda x: isinstance(x, int))
    or_filters['b'] = Filter(lambda x: isinstance(x, str))

    # assert True
    assert or_filters.apply(2)
    
    # XOR collection
    xor_filters = XorFilters()

    #same as 
    xor_filters = Filters(LogicGate.XOR)
    # ---

    xor_filters['a'] = Filter(lambda x: isinstance(x, bool))
    xor_filters['b'] = Filter(lambda x: isinstance(x, str))

    # assert True
    assert xor_filters.apply(False)

    # another logic collections
    xnor_filters = XnorFilters()
    nor_filters = NorFilters()
    nand_filters = NandFilters()

Supported logic gates

from enum import StrEnum


class LogicGate(StrEnum):
    """
    Enum of logic gate
    """
    AND = 'and'
    OR = 'or'
    XOR = 'xor'
    XNOR = 'xnor'
    NAND = 'nand'
    NOR = 'nor'

It is possible to select certain filters of collection

from filterutil import Filter, Filters, OrFilters


def test_func():
    and_filters = Filters(
        a=Filter(lambda x: x == 2), 
        b=Filter(lambda x: isinstance(x, int)),
        c=Filter(lambda x: isinstance(x, str)),
    )
    assert and_filters.apply(2, filter_names=['a', 'b'])
    assert not and_filters.apply(2, filter_names=['b', 'c'])

    or_filters = OrFilters(
        a=Filter(lambda x: x == 1), 
        b=Filter(lambda x: isinstance(x, int)),
        c=Filter(lambda x: isinstance(x, str)),
    )
    assert or_filters.apply(2, filter_names=['a', 'b'])
    assert not or_filters.apply(2, filter_names=['a', 'c'])

Apply another logic gate for the collection dinamically

from filterutil import Filter, Filters, OrFilters, LogicGate


def test_func():
    and_filters = Filters(
        a=Filter(lambda x: x == 1), 
        b=Filter(lambda x: isinstance(x, int)),
    )
    assert not and_filters.apply(2)
    assert and_filters.apply_or(2)
    # or
    assert and_filters.apply(2, logic_gate=LogicGate.OR)


    or_filters = OrFilters(
        a=Filter(lambda x: x == 1), 
        b=Filter(lambda x: isinstance(x, int)),
    )
    assert or_filters.apply(2)
    assert or_filters.apply_xor(2)
    assert not or_filters.apply_and(2)

Infinite nesting of collections

from filterutil import Filter, Filters, OrFilters, LogicGate


def test_func():
    and_filters = Filters(
        LogicGate.AND,
        a=Filter(lambda x: isinstance(x, int)),
        b=Filter(lambda x: isinstance(x, str)),
    )
    xor_filters = Filters(
        LogicGate.XOR,
        a=Filter(lambda x: x == 1),
        b=Filter(lambda x: isintance(x, int)),
    )

    two_collections_in_one = OrFilters(
        first=and_filters,
        second=xor_filters,
    )
    # assert True
    assert two_collections_in_one.apply(2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filterutil-2.0.2.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

filterutil-2.0.2-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file filterutil-2.0.2.tar.gz.

File metadata

  • Download URL: filterutil-2.0.2.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for filterutil-2.0.2.tar.gz
Algorithm Hash digest
SHA256 ccd2c3afb6b4513ad03f9d1103d93e3846a43e773c49173bda59ba56fd15130a
MD5 1d6e77760cd701745da01b64cd8b04bd
BLAKE2b-256 6f0592f7460524dda4d661215f70f488a893086918e0c2dac0a7bea7ee388b09

See more details on using hashes here.

File details

Details for the file filterutil-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: filterutil-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for filterutil-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 75665b89413926b0ea867c64667310cbc15cfe9d5d22f16c84dc02220bb10f77
MD5 06e8e13214c18697415b701fcf1383cf
BLAKE2b-256 efdd98e7b96ae392cd9170621788481f3d72f88222c982ea4a23f424783a1fcf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page