Skip to main content

Functional-style recursive pattern matching in Python. Crazy stuff.

Project description

Functional-style recursive pattern matching in Python. Crazy stuff.

Install

Easy:

pip install patmat

Usage

A quick overview of pattern matching with patmat:

>>> from patmat import *
>>> Mimic({(1, Val('k')): (3, Val('v'))}).match({(1, 2): (3, 4)})
{'k': 2, 'v': 4}

Multiple dispatch generic functions:

>>> from patmat import *
>>> @case
>>> def func(match, l=[Val('head'), ...]):
...     print('a list with first item: {}'.format(match.head))
>>> @case
>>> def func(match, l=Val('item')):
...     print('an item: {}'.format(match.item))
>>> func([1, 2, 3])
a list with first item: 1
>>> func(4)
an item: 4

Matches list, tuple, dict, types, classes with attributes. Brace yourself for the power of recursive pattern matching:

>>> from patmat import *
>>> m = Mimic([
...     1, Type(int, Val(2)),
...     Mimic(a=3, b=[4, Val(5), 6], c=Val(7)),
...     Val(8), {Val(9): 10, Val(11): 12},
... ])
>>> class A:
...     __init__ = lambda self, **kwargs: self.__dict__.update(kwargs)
>>> m.match([1, 2, A(a=3, b=[4, 5, 6], c=7), 8, {9: 10, 11: 12}])
{2: 2, 5: 5, 7: 7, 8: 8, 9: 9, 11: 11}

Project details


Download files

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

Files for patmat, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size patmat-1.0.0.tar.gz (5.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page