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.

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


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-0.2.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

py_predicate-0.2-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_predicate-0.2.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for py_predicate-0.2.tar.gz
Algorithm Hash digest
SHA256 997e47ce05ffdb68cb802d012ed7848e0f95046b165f0f983276cf986d29ce07
MD5 871e1b1331c1135aa6f1af1f433a2c5a
BLAKE2b-256 8d130285322ec8bbc81d2fb0b9897cf95b6f167f8ff8ddd79bbe21af8847fbd4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_predicate-0.2-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for py_predicate-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2616d2f9a6ef422674445ce98e28496fbf8ac06dd9736c5d513d386da5ad4550
MD5 55c585f46966961d934ce9b385425941
BLAKE2b-256 37c34d7fd782dd9c56a3ee72ace637582ea5d8fbbb2eb8b6cca154373f199a0b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page