• Stars
    star
    162
  • Rank 232,284 (Top 5 %)
  • Language
    Python
  • License
    Other
  • Created about 11 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python pattern matching like functional languages.

Python Pattern Matching

Python, I love you. But I'd like you to change. It's not you, it's me. Really. See, you don't have pattern matching. But, that's not the root of it. Macros are the root of it. You don't have macros but that's OK. Right now, I want pattern matching. I know you offer me if/elif/else statements but I need more. I'm going to abuse your functions. Guido, et al, I hope you can forgive me. This will only hurt a little.

Python Pattern Matching is an Apache2 licensed Python module for pattern matching like that found in functional programming languages. Most projects that address Python pattern matching focus on syntax and simple cases. Operator overloading is often used to change the semantics of operators to support pattern matching. In other cases, function decorators are used to implement multiple dispatch, sometimes known as function overloading. Each of these syntaxes, operators, and decorators, is really more of a detail in the application of pattern matching.

A lot of people have tried to make this work before. Somehow it didn't take. I should probably call this yet-another-python-pattern-matching-module but "yappmm" doesn't roll off the tongue. Other people have tried overloading operators and changing codecs. This module started as a codec hack but those are hard because they need an ecosystem of emacs-modes, vim-modes and the like to really be convenient.

Python Pattern Matching focuses instead on the semantics of pattern matching in Python. The dynamic duck-typing behavior in Python is distinct from the tagged unions found in functional programming languages. Rather than trying to emulate the behavior of functional pattern matching, this project attempts to implement pattern matching that looks and feels native to Python. In doing so the traditional function call is used as syntax. There are no import hooks, no codecs, no AST transforms.

.. todo::

   Python ``match`` function example.

Finally, pythonic pattern matching! If you've experienced the feature before in "functional" languages like Erlang, Haskell, Clojure, F#, OCaml, etc. then you can guess at the semantics.

.. todo::

   Show the same code without ``patternmatching``.


Features

  • Pure-Python
  • Developed on Python 3.9
  • Tested on CPython 3.6, 3.7, 3.8, and 3.9
.. todo::

   - Fully Documented
   - 100% test coverage
   - Hours of stress testing


Quickstart

Installing Python Pattern Matching is simple with pip:

$ pip install patternmatching

You can access documentation in the interpreter with Python's built-in help function. The help works on modules, classes, and functions in pattern matching.

>>> from patternmatching import match, bind, bound, like
>>> help(match)                     # doctest: +SKIP

Alternative Packages

Other Languages

Developer Guide

Project Links

Python Pattern Matching License

Copyright 2015-2021, Grant Jenks

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

python-sortedcontainers

Python Sorted Container Types: Sorted List, Sorted Dict, and Sorted Set
Python
3,494
star
2

free-python-games

Free Python Games
Python
3,408
star
3

python-diskcache

Python disk-backed cache (Django-compatible). Faster than Redis and Memcached. Pure-Python.
Python
2,331
star
4

blue

The slightly less uncompromising Python code formatter.
Python
389
star
5

python-wordsegment

English word segmentation, written in pure-Python, and based on a trillion-word corpus.
Python
365
star
6

py-tree-sitter-languages

Binary Python wheels for all tree sitter languages.
Python
149
star
7

python-sortedcollections

Python Sorted Collections Library
Python
102
star
8

python-runstats

Python module for computing statistics and regression in a single pass.
Python
96
star
9

python-tribool

Python implementation of Tribool data type.
Python
20
star
10

gpt-prompt-notes

GPT Prompt Notes
15
star
11

scikit-sequitur

SciKit Sequitur is an Apache2 licensed Python module for inferring compositional hierarchies from sequences.
Python
9
star
12

largefile

Python large file utilities inspired by GNU Coreutils and functional programming.
Python
7
star
13

django-modelqueue

Task queue based on Django models.
Python
7
star
14

django-replay

Django application that records and replays web requests.
Python
4
star
15

django-rrweb

Django application to record and replay browser sessions.
Python
3
star
16

prioritydict

PriorityDict is an Apache2 licensed implementation of a dictionary which maintains key-value pairs in value sort order.
Python
3
star
17

advent-of-code

Advent of Code https://adventofcode.com/
Python
2
star
18

python-ivenv

Interactive virtual environments for Python.
Python
2
star
19

syntaxforest

Python
2
star
20

django-dblog

Django Database Logs
Python
2
star
21

dotemacs

Emacs Lisp
2
star
22

python-kmp

Python implementation of Knuth-Morris-Pratt algorithm for sequence search.
Python
2
star
23

python-appstore

User-oriented front-end for pip.
Python
2
star
24

python-fount

Python
2
star
25

emacs-starbuck

Emacs Lisp
1
star
26

csvcompare

CSV comparison and diff tools.
Python
1
star
27

python-shims

Shims is an Apache2 licensed Python module with patching and mocking utilities.
Python
1
star
28

python-templates

Python templating library with templates included.
Python
1
star
29

python-qemu-python

C
1
star
30

SynDiff

Syntactic differencing tool.
1
star
31

python-prique

Python
1
star
32

django-codemirror6

Django CodeMirror 6
JavaScript
1
star
33

Sequitur

Sequitur is a program for identifying structure in long sequences.
1
star
34

jupyter-nbconvert-blue

Use "blue" to format Python cells in Jupyter notebooks.
1
star