Predicates for Python.
Project description
Predicates for Python
Library for constuction predicates like (OwnedBy('alex') | OwnedBy('alice')) & HasTag('game-design')
and applying them to other objects.
Motivation
I tired from reimplementation of custom predicate logic in my pet projects. So, I implemented that library.
Library focus on usability, not performance. At least, for now.
Install
pip install pydicates
Use
from pydicates import Predicate, common
def HasTag(tag):
return Predicate('has_tag', tag)
def has_tag(context, tag, document):
return tag in document['tags']
common.register('has_tag', has_tag)
document = {'tags': ('a', 'b', 'c', 'd')}
assert common(HasTag('a') & HasTag('c'), document)
assert not common(HasTag('a') & HasTag('e'), document)
assert common(HasTag('a') & ~HasTag('e'), document)
assert common(HasTag('a') & (HasTag('e') | HasTag('d')), document)
More examples can be found in ./examples directory.
See ./examples/documents_check.py for API description.
See ./tests for more examples.
Limitations
- Can not chain redefined comparisons:
a < b < c
is equal to(a < b) and (b < c)
which translates by Python tob < c
, becausea < b
is object (Predicate) and alwaysTrue
.
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
pydicates-0.1.1.tar.gz
(5.2 kB
view hashes)
Built Distribution
Close
Hashes for pydicates-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fd819985839ba5535ea8a296c0a64a14a414ab68b0864a21f3e281d7b607f7d |
|
MD5 | 3cff5df206c9df88fbb71f5fc18deb12 |
|
BLAKE2b-256 | c7fa5c22543b297f2f5ccb5c8aa27c28526c98f4e5379632533ad0323e02b999 |