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, 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-0.5.tar.gz (22.1 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-0.5-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_predicate-0.5.tar.gz
  • Upload date:
  • Size: 22.1 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.5.tar.gz
Algorithm Hash digest
SHA256 e8df2f009ac1f1e10bcdf5aa661fc581e79cdd4cf30cd6e696f6f135880bc7c8
MD5 0a22f691e64c6a9cd465f005271eab67
BLAKE2b-256 e43f41409183e9799fd2660de8c4d514429974f69cae6a48c97ac49b6fff9603

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_predicate-0.5-py3-none-any.whl
  • Upload date:
  • Size: 34.1 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8dc593620ca34d82ad6336d20ba58bcf5a51ccaa56e75ac5dd172b5d79a7038c
MD5 1c2c209c50937d6fe4089459198c4ab8
BLAKE2b-256 98d2f9aecb16756149248d6a88f0ff92e0c61f3e92d0484c618cc68b79d2289a

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