Metadata-Version: 2.4
Name: py_predicate
Version: 1.0
Summary: Module to create composable predicates
Author-email: Maurits Rijk <maurits.rijk@gmail.com>
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python
Classifier: Topic :: Internet
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development
Classifier: Typing :: Typed
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Web Environment
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.12
License-File: LICENSE
Requires-Dist: exrex
Requires-Dist: graphviz
Requires-Dist: lark
Requires-Dist: more-itertools
Requires-Dist: typer
Requires-Dist: bumpversion ; extra == "dev"
Requires-Dist: jsonschema ; extra == "dev"
Requires-Dist: pre-commit ; extra == "dev"
Requires-Dist: black ; extra == "test"
Requires-Dist: mypy ; extra == "test"
Requires-Dist: mypy_extensions ; extra == "test"
Requires-Dist: pytest ; extra == "test"
Requires-Dist: pytest-benchmark ; extra == "test"
Requires-Dist: pytest-cov ; extra == "test"
Requires-Dist: ruff ; extra == "test"
Project-URL: Documentation, https://mrijk.github.io/py-predicate/
Project-URL: Source, https://github.com/mrijk/py-predicate
Provides-Extra: dev
Provides-Extra: test

![Documentation](https://github.com/mrijk/py-predicate/actions/workflows/pages.yaml/badge.svg)
![Test](https://github.com/mrijk/py-predicate/actions/workflows/test.yaml/badge.svg)
[![codecov](https://codecov.io/gh/mrijk/py-predicate/graph/badge.svg?token=KMBDJNC3W9)](https://codecov.io/gh/mrijk/py-predicate)

# Introduction

py-predicate is a typed Python library to create composable predicates.

# Getting started

To get started, install the library with [pip](https://pip.pypa.io/en/stable/)

```
pip install py-predicate
```

The full documentation can be found [here](https://mrijk.github.io/py-predicate/). We give 2 small examples
to show what the library can do.

# Example 1

```python
filtered = [x for x in range(10) if x >= 2 and x <= 3]
```

## Version with predicates:

```python
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.

So lets do just that, reuse our predicate to create a generator. The ``generate_false`` will create an infinite
series of integers for which the predicate ``between_2_and_3`` is False. The ``generate_true`` will create an
infinite (well, with lots of duplicates obviously) series of integers for which the predicate is True.

```python
from predicate import generate_false, generate_true
from more_itertools import take

take(5, generate_true(between_2_and_3))

take(5, generate_false(between_2_and_3))
```

This might be useful for example in unit tests.

# 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.

```python
from predicate import all_p, is_list_p, is_str_p, root_p

str_or_list_of_str = is_str_p | (is_list_p & all_p(root_p))
```

Using plain Python, the above one-liner would have to be coded as a (recursive) function.

