Positional subpattern matching for custom classes.
Project description
Positional subpattern matching for custom classes.
Requirements
Python 3.8 or higher.
Note: This package itself does not require Python 3.10, but its usage only makes sense with the new pattern matching feature introduced in Python 3.10.
Installation
pip install posmatch
Usage
Example 1
Using the pos_match decorator.
from posmatch import pos_match
@pos_match
class Color:
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
color = Color(64, 64, 64)
match color:
case Color(r, g, b) if r == g == b:
print('shade of grey')
case _:
print('other color')
Result:
shade of grey
Example 2
Using the PosMatchMeta metaclass.
from posmatch import PosMatchMeta
class Color(metaclass=PosMatchMeta):
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
color = Color(0, 0, 64)
match color:
case Color(0, 0, b):
print('shade of blue')
case _:
print('other color')
Result:
shade of blue
Example 3
Using the PosMatchMixin mix-in class.
from posmatch import PosMatchMixin
class Color(PosMatchMixin):
def __init__(self, r, g, b):
super().__init__()
self.r = r
self.g = g
self.b = b
color = Color(255, 0, 0)
match color:
case Color(r, 0, 0):
print('shade of red')
case _:
print('other color')
Result:
shade of red
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file posmatch-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: posmatch-0.3.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d4ff26305593ebf82e5eb3a7da869c3112016da3c1489119a81e7eb8b5b7026 |
|
MD5 | 0fbc853b855a65e8ab04397fbea02865 |
|
BLAKE2b-256 | 98afd49425a2dfa743a441645a21d229cf69dc75318d1be718746a8668b0653f |