Module to create composable predicates
Project description
Introduction
py-predicate is a typed Python library to create composable predicates.
Getting started
To get started, install the library with pip
pip install py-predicate
The full documentation can be found here. We give 2 small examples to show what the library can do.
Example 1
filtered = [x for x in range(10) if x >= 2 and x <= 3]
Version with predicates:
from predicate import ge_p, le_p
ge_2 = ge_p(2)
le_3 = le_p(3)
between_2_and_3 = ge_2 & le_3
filtered = [x for x in range(10) if between_2_and_3(x)]
Of course this example looks way more complicated than the original version. The point here is that you can build reusable predicates that can be used in multiple locations.
Example 2
A unique (?) py-predicate feature is that you can define self referencing predicates. This makes it easy to apply predicates to arbitrarily nested structures, like JSON data.
In the next example we define a predicate, that tests if a given data structure is either a string, or a list of data that can again either be a string or a list of data. Ad infinitum.
from predicate import all_p, is_list_p, is_str_p, lazy_p
str_or_list_of_str = is_str_p | (is_list_p & all_p(lazy_p("str_or_list_of_str")))
Using plain Python, the above one-liner would have to be coded as a (recursive) function.
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
Built Distribution
File details
Details for the file py_predicate-0.3.tar.gz
.
File metadata
- Download URL: py_predicate-0.3.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3dee84c30ae88f81b59ac423a5911953e7c18abedf5142587d1af412c0a7a2b |
|
MD5 | ae67bf9140681771c0c601a3153ee063 |
|
BLAKE2b-256 | 05d92ac321fe755a67e0693ed84da3a8614322f9b502b52543b52f00971c9465 |
File details
Details for the file py_predicate-0.3-py3-none-any.whl
.
File metadata
- Download URL: py_predicate-0.3-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ebedc15f4ca2db3bc37d9556d0734ec83cd51e236f382709e7b52e55a900165 |
|
MD5 | e63450bb6c5a5b27c61c1d57d58491eb |
|
BLAKE2b-256 | b78044cefdbab2f0a8cb81707763925ab7d2552a8b7b8eba18ac1ef837800a18 |