Skip to main content

Module to create composable predicates

Project description

Documentation Test codecov

Introduction

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

These predicates can be used for generating test-cases (property based testing) and run-time instrumenting of functions against a specification based on 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.

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.

from predicate import generate_false, generate_true, ge_p, le_p
from more_itertools import take

ge_2 = ge_p(2)
le_3 = le_p(3)

between_2_and_3 = ge_2 & le_3

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.

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.

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

py_predicate-1.6.0.tar.gz (49.6 kB view details)

Uploaded Source

Built Distribution

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

py_predicate-1.6.0-py3-none-any.whl (83.2 kB view details)

Uploaded Python 3

File details

Details for the file py_predicate-1.6.0.tar.gz.

File metadata

  • Download URL: py_predicate-1.6.0.tar.gz
  • Upload date:
  • Size: 49.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for py_predicate-1.6.0.tar.gz
Algorithm Hash digest
SHA256 ffcbc16b91717c412631958115793ea91ae20b9ecdfc61f9c7d7ecab437d5221
MD5 5230dc6dfbb7a8fcc1b0bd7122040ff3
BLAKE2b-256 c5a0b1228ff893a44803ef251e2bf6e0a6a35db7bb3c2f093e0b90a0825be621

See more details on using hashes here.

File details

Details for the file py_predicate-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: py_predicate-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 83.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for py_predicate-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7eba9bba25b7e7776eb78fe5999c9fccab688aa0a4222bf9f3a21bdda8bbc520
MD5 835e327b3134ac98e2a1f9ec5dc9144d
BLAKE2b-256 fbdf0f0e3e31a29d0f20beff87ce00fea774cc398f405ebfc1a9a1b2d7b48dd7

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