Skip to main content

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

pfylter-0.1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

pfylter-0.1.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file pfylter-0.1.1.tar.gz.

File metadata

  • Download URL: pfylter-0.1.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for pfylter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c3b7a897783f0aebd7f9042e5622ea974c751decab5b40164d7eb040dfb3d73b
MD5 cf6d4b3b4a1e35feae7241af7ad5dc0f
BLAKE2b-256 3d6f6a59578b84a6a424764ab59856fca5a234ed2ab87fb0e1b5020a211b0045

See more details on using hashes here.

File details

Details for the file pfylter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pfylter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for pfylter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a6b19df2000f49453ec8fa9642dc089d99efa026b20e12346753ac49f2b8c0bc
MD5 f6b73e9102e03f14dd1952332d3878cf
BLAKE2b-256 61b9de931195606e6816e79e9b4ed9d07e83fc70ebaaded8193eed4c86f5399a

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