effective and graceful pattern matching for original python
Project description
Efficient pattern matching for standard python.
Pattern-Matching
The library name destruct has been registered at PyPI, so we rename Destruct.py as pattern-matching. The new one could be more accurate.
Install
pip install -U pattern-matching.
Example
Pattern Matching for functions.
We can overload the functions easily.
from pattern_matching import Match, when, var, T, t, match_err, _, overwrite
@when(_ == 1, var[int])
def u_func(res):
return res
@when(var < 0, _)
def u_func():
raise varueError('input should be ')
@when(var[int] > 1, var)
def u_func(now, res):
return u_func(now-1, res*now)
@when(var[int])
def u_func(now):
return u_func(now, 1)
u_func(10, 1) # => 3628800
Explicit pattern matching.
with Match(1, 2, (3, int)) as m:
for a, b in m.case((var[int], var, var[list])):
print(a, b)
for typ, in m.case((_, _, (_, var.when(is_type)))):
print(typ)
# => <class 'int'>
Document
See the document here.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for pattern_matching-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c761bfe5dd81f360879ae5180c8fdc18fe267c4287a088c6c77d80734f11765 |
|
MD5 | f4ba6f4f5cf0ea3487b9a70cc858a33c |
|
BLAKE2b-256 | dc33b9d32cf985d26ac44015c5b24aac5a162486f3bf8b615d78f276b792e2e3 |