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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file py_matching_pattern-0.0.3.tar.gz.

File metadata

  • Download URL: py_matching_pattern-0.0.3.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.5

File hashes

Hashes for py_matching_pattern-0.0.3.tar.gz
Algorithm Hash digest
SHA256 943b3fb49bbc29535a3746b1237626fb4c3f64b35ef1840988ee7c269b901de0
MD5 d9686e86aedf3072a5e79a5fc77ae612
BLAKE2b-256 f437777ee10a7424ed71222934f34b7b57a321722e17a20a63b47b06bd8e8bfa

See more details on using hashes here.

File details

Details for the file py_matching_pattern-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: py_matching_pattern-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.5

File hashes

Hashes for py_matching_pattern-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dfd91641f8ad6eaa0121075806d2f3ddf139c1c708e7a3a36c6ae935808bf3c7
MD5 f5c6b3c1d09cfd7335b0f2a35c3afef0
BLAKE2b-256 e3fd528c992377fa48a5f48fe7ea0d3e56205b0d2db0c84b843a8e9e8b9cd5b3

See more details on using hashes here.

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