Python Pattern Matching
Project description
Python Pattern Matching and Object Validation
Reusable pattern matching for Python, implemented in Cython. I originally developed this system for the Ibis Project but hopefully it can be useful for others as well.
The implementation aims to be as quick as possible, the pure python implementation is already quite fast but taking advantage of Cython allows to mitigate the overhead of the Python interpreter. I have also tried to use PyO3 but it had higher overhead than Cython. The current implementation uses the pure python mode of cython allowing quick iteration and testing, and then it can be cythonized and compiled to an extension module giving a significant speedup.
Library components
The library contains three main components which can be used independently or together:
1. Deferred object builders
These allow delayed evaluation of python expressions given a context:
In [1]: from koerce import var, resolve
In [2]: a, b = var("a"), var("b")
In [3]: expr = (a + 1) * b["field"]
In [4]: expr
Out[4]: (($a + 1) * $b['field'])
In [5]: resolve(expr, {"a": 2, "b": {"field": 3}})
Out[5]: 9
The syntax sugar provided by the deferred objects allows the definition of complex object transformations in a concise and natural way.
2. Pattern matchers which operate on various Python objects
Patterns are the heart of the library, they allow searching for specific structures in Python objects. The library provides an extensible yet simple way to define patterns and match values against them.
In [1]: from koerce import match, NoMatch, Anything
In [2]: context = {}
In [3]: match([1, 2, 3, int, "a" @ Anything()], [1, 2, 3, 4, 5], context)
Out[3]: [1, 2, 3, 4, 5]
In [4]: context
Out[4]: {'a': 5}
In [1]: from dataclasses import dataclass
In [2]: @dataclass
...: class B:
...: x: int
...: y: int
...: z: float
...:
In [3]: match(Object(B, y=1, z=2), B(1, 1, 2))
Out[3]: B(x=1, y=1, z=2)
In [4]: Object(B, y=1, z=2)
Out[4]: ObjectOf2(<class '__main__.B'>, 'y'=EqValue(1), 'z'=EqValue(2))
where the Object
pattern checks whether the passed object is
an instance of B
and value.y == 1
and value.z == 2
ignoring
the x
field.
Patterns can also be constructed from python typehints:
In [1]: from koerce import match, CoercionError
In [2]: class Ordinary:
...: def __init__(self, x, y):
...: self.x = x
...: self.y = y
...:
...:
...: class Coercible(Ordinary):
...:
...: @classmethod
...: def __coerce__(cls, value):
...: if isinstance(value, tuple):
...: return Coercible(value[0], value[1])
...: else:
...: raise CoercionError("Cannot coerce value to Coercible")
...:
In [3]: match(Ordinary, Ordinary(1, 2))
Out[3]: <__main__.Ordinary at 0x105194fe0>
In [4]: match(Ordinary, (1, 2))
Out[4]: koerce.patterns.NoMatch
In [5]: match(Coercible, (1, 2))
Out[5]: <__main__.Coercible at 0x109ebb320>
The pattern creation logic also handles generic types by doing
lightweight type parameter inference. The implementation is quite
compact, available under Pattern.from_typehint()
.
3. A high-level validation system for dataclass-like objects
This abstraction is similar to what attrs or pydantic provide but there are some differences (TODO listing them).
In [1]: from typing import Optional
...: from koerce import Annotable
...:
...:
...: class MyClass(Annotable):
...: x: int
...: y: float
...: z: Optional[list[str]] = None
...:
In [2]: MyClass(1, 2.0, ["a", "b"])
Out[2]: MyClass(x=1, y=2.0, z=['a', 'b'])
In [3]: MyClass(1, 2, ["a", "b"])
Out[3]: MyClass(x=1, y=2.0, z=['a', 'b'])
In [4]: MyClass("invalid", 2, ["a", "b"])
Out[4]: # raises validation error
Annotable object are mutable by default, but can be made immutable
by passing immutable=True
to the Annotable
base class. Often
it is useful to make immutable objects hashable as well, which can
be done by passing hashable=True
to the Annotable
base class,
in this case the hash is precomputed during initialization and
stored in the object making the dictionary lookups cheap.
In [1]: from typing import Optional
...: from koerce import Annotable
...:
...:
...: class MyClass(Annotable, immutable=True, hashable=True):
...: x: int
...: y: float
...: z: Optional[tuple[str, ...]] = None
...:
In [2]: a = MyClass(1, 2.0, ["a", "b"])
In [3]: a
Out[3]: MyClass(x=1, y=2.0, z=('a', 'b'))
In [4]: a.x = 2
AttributeError: Attribute 'x' cannot be assigned to immutable instance of type <class '__main__.MyClass'>
In [5]: {a: 1}
Out[5]: {MyClass(x=1, y=2.0, z=('a', 'b')): 1}
TODO:
The README is under construction, planning to improve it:
- More advanced matching examples
- Add benchmarks against pydantic
- Show variable capturing
- Show match and replace in nested structures
- Example of validating functions by using @annotated decorator
- Explain
allow_coercible
flag - Mention other relevant libraries
Other examples
from koerce import match, NoMatch
from koerce.sugar import Namespace
from koerce.patterns import SomeOf, ListOf
assert match([1, 2, 3, SomeOf(int, at_least=1)], four) == four
assert match([1, 2, 3, SomeOf(int, at_least=1)], three) is NoMatch
assert match(int, 1) == 1
assert match(ListOf(int), [1, 2, 3]) == [1, 2, 3]
from dataclasses import dataclass
from koerce.sugar import match, Namespace, var
from koerce.patterns import pattern
from koerce.builder import builder
@dataclass
class A:
x: int
y: int
@dataclass
class B:
x: int
y: int
z: float
p = Namespace(pattern, __name__)
d = Namespace(builder, __name__)
x = var("x")
y = var("y")
assert match(p.A(~x, ~y) >> d.B(x=x, y=1, z=y), A(1, 2)) == B(x=1, y=1, z=2)
More examples and a comprehensive readme are on the way.
Packages are not published to PyPI yet.
Python support follows https://numpy.org/neps/nep-0029-deprecation_policy.html
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
Built Distributions
Hashes for koerce-0.1.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5ff2a9e90ab3a89b952f4560a581137a403d37137afb1cd6fabcf38e31a8055 |
|
MD5 | 7b91a10daceb18241eac9b6384a9ebee |
|
BLAKE2b-256 | 3f058ae51ad515b294850cd228c5376b64eda621ec24ff9c9aec7ae70c037199 |
Hashes for koerce-0.1.0-cp312-cp312-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91c12e81eea22dcddaa695fe1914933104c2ea691e16303398e30fc475113005 |
|
MD5 | 87e0af68742d1c39959c7c6e87ad7847 |
|
BLAKE2b-256 | f0a2f00a25dd878e0b5562006cd686db60042e76ccbc0c258c3c8a84357dffe1 |
Hashes for koerce-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d87ab6521a21eb1e96f758dc1fd400e8e731255548d2e0ef75900949e2e5219 |
|
MD5 | 39697b0cbd7c01a8d17ed85701b66889 |
|
BLAKE2b-256 | e097ba07c0f5e560aa0ac4dbb764a2fc172152a589bbb3efcd3337781417e6b8 |
Hashes for koerce-0.1.0-cp312-cp312-musllinux_1_2_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 058abc2546016f244e11455ce2e89ea2fbf963753b03098684d92c8c810fba2c |
|
MD5 | 159c583679e715640cc430c1bd151e2d |
|
BLAKE2b-256 | 956109b1ca2d42957e126c6cc07a6af981f345c7af1b5d9d10da6bc5d044b128 |
Hashes for koerce-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 072d6218981d1c7a8dada11ab52c82b2002bf02fdb200e720f445e14c7f35505 |
|
MD5 | 6ea772dd9b892f10390865759d0a5015 |
|
BLAKE2b-256 | 4d33449b2c6a1474387b29cf039df0b78ae9419e3e4f0a84712dcf3fcd365a23 |
Hashes for koerce-0.1.0-cp312-cp312-manylinux_2_17_i686.manylinux_2_5_i686.manylinux1_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 996982a1c01e5977819b9be8ae09146d36ccc0ded055f0fe8d720c828f1e89aa |
|
MD5 | c1862575a5d55c4d2d7a345e6d13a466 |
|
BLAKE2b-256 | eb542725ddededb13fa3159e81c4ba5b9b94aee680a51878972b2064119d5628 |
Hashes for koerce-0.1.0-cp312-cp312-macosx_14_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b1aee978ae0ad2feb36f2ef7a1d4616df6952e80a44da96a3bcf1c4cb9ec12f |
|
MD5 | bc0164686673151b6fca93596aa59c72 |
|
BLAKE2b-256 | 9829a71c0bb99f57c748623e20a986f0b6e809181fba450ada4db7e089fb96a8 |
Hashes for koerce-0.1.0-cp312-cp312-macosx_13_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3149ad9d4d0d9c5813586be080efea1bb7c83a8aafb286ad7295c2f050ca4b9 |
|
MD5 | d8e41193a881d97cc0f854f0a789ccf3 |
|
BLAKE2b-256 | 09e74551afb68a68c7cb3ac38c7b2fdc17d068a54d1d813f46ae8b36d5d12125 |
Hashes for koerce-0.1.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 884248b0efee75d6ecd8c578bae679b1557a6179906b961cac91669dfa08c4c4 |
|
MD5 | bdf91143b3a6a10e8ad0e4c1781ae120 |
|
BLAKE2b-256 | 60c1d5ab157756fa7ea5a00ee39419f4e59594afd7e3ee7834d98cfc186fb8a6 |
Hashes for koerce-0.1.0-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1185b99e344611577be7c7d3af3a578ee6eac886e9a87067c95ce34bb68147e6 |
|
MD5 | c742e48297037005c1417e9658b50f9a |
|
BLAKE2b-256 | 38b846bda3c6782c340bc1cd5af868443ea475c8c738a84105cae0edfe83dd66 |
Hashes for koerce-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89c31fc1685e2796421f3d73a160861699e094542f23616ad072dd48c4f0b9fe |
|
MD5 | 85894f953e00243644ef987164bc8a95 |
|
BLAKE2b-256 | cd5219e5a02573918ea2cb353d79fd1f4297e813ff388ae687c16b957c6c3331 |
Hashes for koerce-0.1.0-cp311-cp311-musllinux_1_2_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76048c5a59cec29ecab388c1c993564c5be6c4fe367c82a7ef944a026cd7ae30 |
|
MD5 | b1725b6194c9af94776cf084f65a152b |
|
BLAKE2b-256 | f4900e58e1e5cbc2e86f6afdb95061841551f4ac86bf193159f0ebf6d8c3f57b |
Hashes for koerce-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1c810fd05ae9175c5a6a33199935a46d39211c3e7d7e623a72c826a8a450806 |
|
MD5 | 53c670a3067a22c69d819f785d9511f4 |
|
BLAKE2b-256 | ff87575cd49272e7524ee88a533a64bf6fd3bbfd8d4e4a16b5d188040323900b |
Hashes for koerce-0.1.0-cp311-cp311-manylinux_2_17_i686.manylinux_2_5_i686.manylinux1_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff36f39c676d8681390c49ec7f9d56f8decfa90071b5c3c87ba5558994de4e5d |
|
MD5 | 87df049c8817e9f27b994ba64f779a8d |
|
BLAKE2b-256 | d1acb667af60c5cd6d7096871c5c51b8fdf3b538623a07b449749b1bb3f98630 |
Hashes for koerce-0.1.0-cp311-cp311-macosx_14_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23a72d1c72aad0cc1e2a9dcc65039e9a679c7a11a86094b4fb53c98c6b35a5b7 |
|
MD5 | 38bba4c2216db7f6cef6774a9e4cbe3a |
|
BLAKE2b-256 | 4fb106518b5d6605381ec4d6d9bf4b6d119c98cb452034d9b64df435e16b32cb |
Hashes for koerce-0.1.0-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf5ab78df7f188b98d82ea20ea744bb172342fe01031d1580130a68c46e5e457 |
|
MD5 | 4e80ceb7666ab0a45fe2ebdb13ce21db |
|
BLAKE2b-256 | 214b98020b35693d51581c7421c89f368e2c29fa8ba055796e3030eb10eb9735 |
Hashes for koerce-0.1.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b82e20c67a3d93c60be0b54bd91179ab301a5a364789d18dd89f158476d653f |
|
MD5 | 0e75dc9d130c34d4f8e87213b36202e9 |
|
BLAKE2b-256 | 17ba76a2a42fb6b16e4032fc6397ee9ac1c8ceceb1748d67892eff98add23166 |
Hashes for koerce-0.1.0-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52bb37c041cfdc7e77e1ff9d3a804f6ef9c12ab4596406c65da344ae8c14ebd6 |
|
MD5 | 02356495534fafdf057b0459ce09dc20 |
|
BLAKE2b-256 | 4ae814291021ad063b938b517cc52b8178aa67188194b3714883773b877dfceb |
Hashes for koerce-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07af9adda7e44706427025be8db6cf40a7a25fe8afb31445cf3970725fb74ca4 |
|
MD5 | 58025b9c88b97b0264ee52a9dd9de2b2 |
|
BLAKE2b-256 | 2a4adda76863e5ff710a8841784f0944681ff466cc57da53d00b448bd88fe560 |
Hashes for koerce-0.1.0-cp310-cp310-musllinux_1_2_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa0eb233273759b14c625d19473aebd2d5ea4388decc47cb46a11a98829fbef0 |
|
MD5 | 14c0e05437092331f62058b4da66862e |
|
BLAKE2b-256 | 363b0574d0f8d26816458e526571298dd77d1660da43fcb3373aa1ee4a1faad3 |
Hashes for koerce-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe8abdf19471275c05a5bb708c56f8b23a331560fa94f877f1cde8e1e0a727d5 |
|
MD5 | f769657b67baafc32b439554e7476cc8 |
|
BLAKE2b-256 | 5007239e15b9bd282cdb20e0b7b988553cfd8ac75c0f602a5957d04899e73e41 |
Hashes for koerce-0.1.0-cp310-cp310-manylinux_2_17_i686.manylinux_2_5_i686.manylinux1_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df9fd39b7b92f90b4c83ceb7f3612c058b2d9b352f63805b57cd85f756379900 |
|
MD5 | af799ad495d34a44ab46a939c73acdaa |
|
BLAKE2b-256 | 75c4373835b039e455d4b2c5959953760f5cc48868a0e0e03f5fd4b9f44c11c1 |
Hashes for koerce-0.1.0-cp310-cp310-macosx_14_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ced60bb1e9cd2843e36abc7f8bb2056cc8e7366ea015c75cb73d82d2beaa418d |
|
MD5 | b6d07422f45cc2cf0f4f5dd401eca386 |
|
BLAKE2b-256 | 1375e4235173c1d6d16e9df77cd6daa05746f9b694f536da7fa733d8f5400e77 |
Hashes for koerce-0.1.0-cp310-cp310-macosx_13_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24249b86d5371aba20efb209a07173dad299e10777ecde38535dbb131772c723 |
|
MD5 | d20890c9b7a49633bf12671c6eacbd28 |
|
BLAKE2b-256 | 36091084178e89bbb420db4ee2a1d458f6aafb08081283193374b42296d5db26 |