Skip to main content

Pattern matching for python

Project description

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

  • better handling of None, True, False matching

  • matching to _ should not capture

  • name parameter

  • better handling of Mismatch passing to function env

  • non-strict dict matching

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patterns-0.2.tar.gz (5.6 kB view details)

Uploaded Source

File details

Details for the file patterns-0.2.tar.gz.

File metadata

  • Download URL: patterns-0.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for patterns-0.2.tar.gz
Algorithm Hash digest
SHA256 f6f8462b59873cbc70a695549405534d1c98628d33db139181742c7237b483ac
MD5 5c3502e520d59b2eadb72fa6c7759246
BLAKE2b-256 2f9fce795d63a9c5ff43360329e9ca2115bc767afed00d7f956ffea8145b91ca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page