Skip to main content

Dynamic Pattern Matching Library

Project description

Python Pattern Matching

Inspired by pampy and core.match, this is a pattern matching library for dynamic list of patterns.

Right now it only supports matching lists of same sizes.

Usage

Import the lib

import PatternMatchStore from py_matching_pattern

First you will add initialize a PatternMatchStore with a fixed size of keys to match (size of the list).

pmdb = PatternMatchStore(keysize=3)

After that you can add the patterns. Every value is valid match, including None.

The "catch all" value is default attribute of the instance.

_ = pmdb.default
pmdb.put(keys=["a","b","c"],value=1)
pmdb.put(keys=["a","b","b"],value=2)
pmdb.put(keys=["a","b",_],value=3)
pmdb.put(keys=["a",_,_],value=4)
pmdb.put(keys=["a",None,"c"],value=5)
pmdb.put(keys=["a",_,"d"],value=6)

These patterns are staged for the DB. This is useful if you reload the patterns from another thread.

To make the pattern live, you commit it.

pmdb.commit()

Now you can finally start matching values:

pmdb.get(keys=["a","b","c"])==1
pmdb.get(keys=["a","b","b"])==2
pmdb.get(keys=["a","b","d"])==3
pmdb.get(keys=["a","c","d"])==4
pmdb.get(keys=["a",None,"c"])==5
pmdb.get(keys=["a","e","d"])==6
pmdb.get(keys=["e","e","e"])==None

The staged patterns are kept between commits, so you can add or override to it later.

But in case you want to clean it:

pmdb.clean()

It can also raise an exception if not found, but by default it will return None:

pmdb = core.PatternMatchStore(keysize=3,raise_on_notfound=True)
pmdb.get(keys=["e","e","e"]) # raise KeyNotFound

License

MIT

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

py_matching_pattern-0.0.3.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

py_matching_pattern-0.0.3-py3-none-any.whl (3.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page