-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,54 @@ | ||
Python Pattern Matching | ||
----------------------- | ||
|
||
Reusable pattern matching for Python, implemented in Cython. | ||
I originally developed this system for the Ibis Project in pure python, | ||
but it can be useful in many other contexts as well. | ||
|
||
Examples | ||
======== | ||
|
||
```python | ||
from koerce.sugar import match, Namespace | ||
from koerce.patterns import SomeOf, NoMatch, 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] | ||
``` | ||
|
||
```python | ||
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 |