Skip to main content

Patterns

Pattern matching for python. Works in Python 2.7, 3.3+ and pypy.

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

  • aliases for structures

  • destructure objects

  • name parameter

  • better handling of Mismatch passing to function env

  • non-strict dict matching

Metadata

Release files for patterns 0.3

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.3
File Size Uploaded Provenance Actions
patterns-0.3.tar.gz 6.0 kB Without provenance Details for patterns-0.3.tar.gz

Release files / patterns-0.3.tar.gz

Download URL patterns-0.3.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
af8144d8993253661fd3d67d52d84d2f4c5ab77c873b17e7c79dd096e04a3049
BLAKE2b-256 checksum
How to use checksums
5e3a6bcb6c21e83bda343cd2e0357d7fb6cfd3a9a099d4c3743c79bf58527723
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for patterns 0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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