A generic filtering framework using abstract composition
Project description
🔍 pfylter
pfylter is a lightweight, flexible, and extensible Python framework for applying composable filters to arbitrary data. It’s built using the composite design pattern, allowing complex logical conditions to be expressed and reused cleanly.
🚀 Features
- ✅ Define your own filters by subclassing
AbstractFilter - ✅ Combine filters using logical AND (
AllFilters) or OR (AnyFilter) - ✅ Support for generic data types (strings, numbers, objects, etc.)
- ✅ Clean, readable syntax using list comprehensions and type hints
- ✅ Perfect for data processing, rule engines, and validation pipelines
📦 Installation
pip install pfylter
✨ Quick Start
1. Define Some Filters
from pfylter import LenFilter, StartsWithFilter
data = ["A", "ABCD", "B", "BCDE", "C", "AAAAAAA"]
# Only keep strings of length 4 that start with 'A'
from pfylter import AllFilters
f = AllFilters([
LenFilter(4),
StartsWithFilter("A")
])
print(f.apply(data)) # ['ABCD']
2. Use OR Logic with AnyFilter
from pfylter import AnyFilter
f = AnyFilter([
LenFilter(4),
StartsWithFilter("A")
])
print(f.apply(data)) # ['A', 'ABCD', 'BCDE', 'AAAAAAA']
🔄 Composing Filters
Because AllFilters and AnyFilter are themselves filters, they can be nested to build complex conditions:
from pfylter import AllFilters, AnyFilter, LenFilter, StartsWithFilter
# Keep strings that start with 'A' and have length 1 or 4
complex_filter = AllFilters([
StartsWithFilter("A"),
AnyFilter([
LenFilter(1),
LenFilter(4)
])
])
print(complex_filter.apply(["A", "ABCD", "AAAAAA", "B"])) # ['A', 'ABCD']
🛠 Creating Custom Filters
You can define custom filters by inheriting from AbstractFilter:
from pfylter import AbstractFilter
class GreaterThanFilter(AbstractFilter[int]):
def __init__(self, threshold: int):
self.threshold = threshold
def keep(self, instance: int) -> bool:
return instance > self.threshold
Now you can use this filter in combination with others!
📝 License
MIT License — see LICENSE file for details.
🤝 Contributing
Feel free to open issues or pull requests. All feedback is welcome!
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
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 pfylter-0.2.0.tar.gz.
File metadata
- Download URL: pfylter-0.2.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bcf3dd428b780f55540114dbc59b137835b2a13945dbd88a8aee2451f9d6e28
|
|
| MD5 |
28f12e983aaa993a45b4377c2660e950
|
|
| BLAKE2b-256 |
4baba9f0393aaaf31850b84c9ae8b654585794c10161b4b988124fb28333243d
|
File details
Details for the file pfylter-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pfylter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfba6041bee9404607d3fc74a34b9663e67a06363ff09d74492d1908fd1d403b
|
|
| MD5 |
689be0963e326b2a65c7ab53dce1c2c3
|
|
| BLAKE2b-256 |
0fbcbbde03858c7ac6b23fff34176f74ff0c7ef2e06210890748a4862f1069cd
|