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.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

posmatch-0.8.1-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: posmatch-0.8.1.tar.gz
  • Upload date:
  • Size: 3.1 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.1.tar.gz
Algorithm Hash digest
SHA256 37ad6b1f911717c90a69339158bd0d623c8fc298ff0d0244286fa2b1f0ff0b9b
MD5 b8d79d9f69a2f4dfd26ff3aa130d38a7
BLAKE2b-256 a9c8c97c2f4ef2e339e297aad8c583a97c08b053df740ffc2d830f2722b1a937

See more details on using hashes here.

File details

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

File metadata

  • Download URL: posmatch-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b57972e274cdc0f1c6848d797bf71c0b25510ebf36780fb1118bb0d92b6a0610
MD5 c50c447e560a6e58dac00198f78168bc
BLAKE2b-256 ceb6c9c879b83c7ab95f972405d4c81da591458c4ae161db2851d87bdc3dfbbf

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