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):

Coupling filters with AND policy

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 policy

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 policy

from filterutil import Filter


def test_func():
    a = Filter(lambda x: x == 1)
    b = Filter(lambda x: x == 3)
    compound_filter = a ^ b

    # assert True
    assert compound_filter.apply(2)

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 policy

from filterutil import Filter, Filters, OrFilters, XorFilters, FilterCouplingPolicy


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(FilterCouplingPolicy.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(FilterCouplingPolicy.XOR)
    # ---

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

    # assert True
    assert xor_filters.apply(False)

Infinite nesting of collections

from filterutil import Filter, Filters, OrFilters, XorFilters, FilterCouplingPolicy


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

    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-0.0.7.tar.gz (8.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-0.0.7-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for filterutil-0.0.7.tar.gz
Algorithm Hash digest
SHA256 cdb7f7b897e259a6c519adb6d6cbbd8f8c292a1db8d97bf3067e0b5dba6fe9cb
MD5 1470b6cfb3b6425d41c628488ce8eb62
BLAKE2b-256 5a1931dc1790314bfc20ab254422ab79a918575470a779342519bd0037b49911

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for filterutil-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 16419a769fb4c3e1a132aba511edc2d662858be284ab1907b45a79a6d0782116
MD5 bc3351e1694e4a1f2415956f06c47849
BLAKE2b-256 88b0db79553217dad30ad874236b10591e1c17d336aee7151be89d746bfd781f

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