Skip to main content

Positional sub-pattern matching for custom classes.

Project description

release pypi

posmatch

Positional sub-pattern matching for custom classes.

Requirements

Python 3.10 or higher.

Installation

pip install posmatch

Usage

The pos_match decorator

from posmatch import pos_match


@pos_match
class Color:
    def __init__(self, red, green, blue):
        self.red = red
        self.green = green
        self.blue = blue


color = Color(0, 0, 128)

match color:
    case Color(r, g, b) if r == g == b:
        print('Shade of grey')
    case Color(0, 0):
        print('Shade of blue')

Output:

Shade of blue

The PosMatchMeta metaclass

from posmatch import PosMatchMeta


class Date(metaclass=PosMatchMeta):
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day


date = Date(2121, 1, 1)

match date:
    case Date(_, m, d) if m == 5 and d == 1:
        print('May Day')
    case Date(y) if y > 2100:
        print('Distant future')

Output:

Distant future

The PosMatchMixin mix-in class

from posmatch import PosMatchMixin


class Rectangle(PosMatchMixin):
    def __init__(self, width, height):
        self.width = width
        self.height = height


shape = Rectangle(16, 16)

match shape:
    case Rectangle(w, h) if w == h:
        print('Square')
    case Rectangle(x, y) if x > y:
        print('Landscape')

Output:

Square

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

posmatch-0.8.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

posmatch-0.8.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file posmatch-0.8.0.tar.gz.

File metadata

  • Download URL: posmatch-0.8.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-35-generic

File hashes

Hashes for posmatch-0.8.0.tar.gz
Algorithm Hash digest
SHA256 87832b7cf3b001a74aa5c8f57e01fe7eee3081552fd631e8bfea3fe8bd72ca92
MD5 fd3b305b11518bf8d1299fa067d2f079
BLAKE2b-256 a33b8da150de5391eba9c6c85625432cf4e2384b99d23e579d86249ca8afb32e

See more details on using hashes here.

File details

Details for the file posmatch-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: posmatch-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-35-generic

File hashes

Hashes for posmatch-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c872ba5740e9e383210dd0c95211ef77b30fac29d5fae9cf642a00c46a41fbe
MD5 0319f0b237446d7aec7d5596f2e42109
BLAKE2b-256 f2cbddf5307cde90c28d0b9139927fc1ee5894a6d340eca21544d0d7e4c51ee5

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