Skip to main content

A pattern matching library.

Project description

A pattern matching libary for python.

Work in progress

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 or Haskell.

Expressions

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

>>> from matchpy.expressions 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 variables and wildcards. Variables can give a name to a node of the pattern so that it can be accessed later. Wildcards are placeholders that stand for any expression. Usually, the two are used in combination:

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

However, unnamed wildcards can also be used:

>>> from matchpy.expressions import Wildcard
>>> w = Wildcard.dot()
>>> print(f(w, w))
f(_, _)

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

>>> print(Variable('y', f(w, a)))
y: f(_, a)

In addition, it supports sequence wildcards that stand for multiple expressions:

>>> z = Variable.plus('z')
>>> print(f(z))
f(z__)

Substitutions

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

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

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

>>> from matchpy.functions import substitute
>>> original, _ = substitute(pattern, substitution)
>>> print(original)
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.1.2.tar.gz (79.0 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.1.2-py3-none-any.whl (54.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for matchpy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7382bb22020def30d9e38c5a61a206012076c0e9af7dc32dace3bbaf7c040b00
MD5 5ca658973a385cf0b69dee49f51d9f65
BLAKE2b-256 d6adb4e6874f57a5d0f3bf1cd5b55d6900761ad0555aef3fa69fb003f6d98e4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for matchpy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 146c4665fa6324ad94371e8d0817a1f9d4d0fb3440d2c5a1591bd31e22265203
MD5 226bc41cac5523471d2a9191bb9ee8a4
BLAKE2b-256 82887ed2bf60ed7124df83317230c08c74c1ff5cf620b856b585bc436cebeb19

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