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 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)
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 coupling policy for the collection dinamically
from filterutil import Filter, Filters, OrFilters, FilterCouplingPolicy
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, coupling_policy=FilterCouplingPolicy.OR)
or_filters = OrFilters(
a=Filter(lambda x: x == 1),
b=Filter(lambda x: isinstance(x, int)),
)
assert or_filters.apply(2)
assert not or_filters.apply_and(2)
assert not or_filters.apply_xor(2)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
filterutil-1.0.3.tar.gz
(9.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file filterutil-1.0.3.tar.gz.
File metadata
- Download URL: filterutil-1.0.3.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78ff3240e9e7bbf8b6e20481b264fc8681d10251a4c9cc18efa57a298bb8503e
|
|
| MD5 |
99ade0607bc148a744b297c2c8808fc4
|
|
| BLAKE2b-256 |
7f9a425ef43b256f747e75689cbda986b1896df90caad6b9726270263f84b80b
|
File details
Details for the file filterutil-1.0.3-py3-none-any.whl.
File metadata
- Download URL: filterutil-1.0.3-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0b5fd0e64bbfc1a8affc5e0bf029595331909eb35e1d018de3892a19ef142f0
|
|
| MD5 |
1aa4d7820902dace045640e2c3505088
|
|
| BLAKE2b-256 |
27e83570dbc266ce2478305f46a2091e9e8cd05ab95e841b31aaadbaf2433c61
|