Skip to main content

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for patterns 0.1.1
File Size Uploaded
patterns-0.1.1.tar.gz 4.8 kB Details

Release files / patterns-0.1.1.tar.gz

Download URL patterns-0.1.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bd83e24544bd02762527a3b43190c9fd669f39756b273fe4bca8346673c66ed7
BLAKE2b-256 checksum
How to use checksums
a6ca0c7b5a26ab6dc27272b38e265878cebea9d41c45c401840f6279022ba197
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.3

1 release file

0.2

1 release file

0.1.2

1 release file

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page