Pattern matching for python. Works in Python 2.7, 3.3+ and pypy.
pip install patterns
frompatternsimportpatterns, Mismatch@patternsdeffactorial(): if0: 1ifnisint: n*factorial(n-1) if []: [] if [x] +xs: [factorial(x)] +factorial(xs) if{'n': n, 'f': f}: f(factorial(n)) assertfactorial(0) ==1assertfactorial(5) ==120assertfactorial([3,4,2]) == [6, 24, 2] assertfactorial({'n': [5, 1], 'f': sum}) ==121factorial('hello') # raises MismatchSee tests for more examples.
- docs
- aliases for structures
- destructure objects
- name parameter
- better handling of
Mismatchpassing to function env - non-strict dict matching