Skip to main content

A pattern matching library.

Project description

MatchPy is a pattern matching libary for python.

Work in progress

Latest version released on PyPi Test coverage Build status of the master branch Documentation Status

Installation

MatchPy is availiablle via PyPI. You can install it using pip install matchpy.

Overview

This package implements pattern matching in python. It is similar to the implementation in Mathematica. A detailed example of how you can use matchpy can be found in the documentation. Some of the implemented algorithms have been described in this Master thesis.

In addition to the basic matching algorithm, there are data structures that can be used for more efficient many-to-one matching like the ManyToOneMatcher and the DiscriminationNet.

Expressions

Expressions and patterns both have a tree structure. Expressions consist of symbols (leafs) and operations (internal nodes):

>>> from matchpy import Operation, Symbol, Arity
>>> f = Operation.new('f', Arity.binary)
>>> a = Symbol('a')
>>> print(f(a, a))
f(a, a)

Patterns are expressions which can additionally contain wildcards and subexpressions can have a variable name assigned to them. During matching, a subject matching a pattern with a variable will be captured so it can be accessed later. Wildcards are placeholders that stand for any expression. Usually, the wildcards are used in combination with a variable name:

>>> from matchpy import Wildcard
>>> x = Wildcard.dot('x')
>>> print(Pattern(f(a, x)))
f(a, x_)

Here x is the name of the variable. However, unnamed wildcards can also be used:

>>> w = Wildcard.dot()
>>> print(Pattern(f(w, w)))
f(_, _)

Or a more complex expression can be named with a variable:

>>> print(Pattern(f(w, a, variable_name='y')))
y: f(_, a)

In addition, sequence wildcards that can match for multiple expressions are supported:

>>> z = Wildcard.plus('z')
>>> print(Pattern(f(z)))
f(z__)

Substitutions

Matches are given in the form of substitutions, which are a mapping from variable names to expressions:

>>> from matchpy import match
>>> y = Wildcard.dot('y')
>>> b = Symbol('b')
>>> expression = f(a, b)
>>> pattern = Pattern(f(x, y))
>>> substitution = next(match(expression, pattern))
>>> substitution
{'x': Symbol('a'), 'y': Symbol('b')}

Replacing the variables in the pattern according to the substitution will yield the original subject expression:

>>> from matchpy import substitute
>>> print(substitute(pattern, substitution))
f(a, b)

Roadmap

Besides the existing features, we plan on adding the following to MatchPy:

  • Support for Mathematica’s Alternatives: For example f(a | b) would match either f(a) or f(b).

  • Support for Mathematica’s Repeated: For example f(a..) would match f(a), f(a, a), f(a, a, a), etc.

  • Support pattern sequences (PatternSequence in Mathematica). These are mainly useful in combination with Alternatives or Repeated, e.g. f(a | (b, c)) would match either f(a) or f(b, c). f((a a)..) would match any f with an even number of a arguments.

  • All these additional pattern features need to be supported in the ManyToOneMatcher as well.

  • Better integration with existing types such as dict.

  • Code generation for both one-to-one and many-to-one matching.

  • Improving the documentation with more examples.

  • Better test coverage with more randomized tests.

Contributing

If you have some issue or want to contribute, please feel free to open an issue or create a pull request. Help is always appreciated!

The Makefile has several tasks to help development:

  • To install all needed packages, you can use make init .

  • To run the tests you can use make test. The tests use pytest.

  • To generate the documentation you can use make docs .

  • To run the style checker (pylint) you can use make check .

If you have any questions or need help with setting things up, please open an issue and we will try the best to assist you.

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

matchpy-0.4.3.tar.gz (98.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matchpy-0.4.3-py3-none-any.whl (68.6 kB view details)

Uploaded Python 3

File details

Details for the file matchpy-0.4.3.tar.gz.

File metadata

  • Download URL: matchpy-0.4.3.tar.gz
  • Upload date:
  • Size: 98.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for matchpy-0.4.3.tar.gz
Algorithm Hash digest
SHA256 46a0b8dab6feea67cdc58438237ef353d069c1d81cc50f159ec1e52aaf58139f
MD5 86c6fd87f06bb8a45e6fb0441e01bf7f
BLAKE2b-256 ccfa196fdf16ee5659a0f1e18d6353087161fba03d99de59d0eb0ca32ece3c85

See more details on using hashes here.

File details

Details for the file matchpy-0.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for matchpy-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 febaeb058f530be634281d7c6e281495df03467b04e1ae1b64706bf4e2a8e927
MD5 92e7e0d0e9919aeb7447bc6c0834d4ea
BLAKE2b-256 c1f18cb48b9aa9e38f4027ba079630622205018a8c6cfc21afbadad1f38f5c02

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