Patterns
Pattern matching for python.
Installation
pip install patterns
Usage
from patterns import patterns, Mismatch
@patterns
def factorial():
if 0: 1
if n is int: n * factorial(n-1)
if []: []
if [x] + xs: [factorial(x)] + factorial(xs)
if {'n': n, 'f': f}: f(factorial(n))
assert factorial(0) == 1
assert factorial(5) == 120
assert factorial([3,4,2]) == [6, 24, 2]
assert factorial({'n': [5, 1], 'f': sum}) == 121
factorial('hello') # raises Mismatch
See tests for more examples.
TODO
docs
matching to _ should not capture
name parameter
better handling of Mismatch passing to function env
non-strict dict matching
Metadata
Release files for patterns 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| patterns-0.1.0.tar.gz | 4.8 kB | Details |
Release files / patterns-0.1.0.tar.gz
| Download URL | patterns-0.1.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e195af22cd661ef956009ffd5a5078b7bd455fbd766ba6afc9b7e39f7a540728
|
|
BLAKE2b-256 checksum How to use checksums |
7f6b6680683c519351b221f3fe7421440c63a3c2610c23a1c099eb198c035183
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |