Skip to main content

A pattern matching library.

Project description

A pattern matching libary for python.

Work in progress

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

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.

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)

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.3.1.tar.gz (87.8 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.3.1-py3-none-any.whl (59.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for matchpy-0.3.1.tar.gz
Algorithm Hash digest
SHA256 fde92a8ef4d45ac3ecb2af7686c27c992ba4e3f3163c06020c54fc8bb44ae344
MD5 9a7be06a11663555fd71c2d79f0619aa
BLAKE2b-256 9f6c79f693341ec816e50f843efe7b5079a990cb0f6f00d53386ec0fd037327e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for matchpy-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c42a15dfb784675364e1cf85c826db39f42f057d940a3e377ad7b9efb1362272
MD5 7a74aab744445abcdd082b61c0ec7199
BLAKE2b-256 a12c222d7b3c0e8089b97da58a82bbc66c6471619347b72565e14d6d4010487f

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