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.

Files for py-matching-pattern, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size py_matching_pattern-0.0.3-py3-none-any.whl (3.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size py_matching_pattern-0.0.3.tar.gz (2.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page