Skip to main content

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

Metadata

Release files for posmatch 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for posmatch 0.8.1
File Size Uploaded
posmatch-0.8.1.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for posmatch 0.8.1
File Interpreter ABI Platform
posmatch-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.7 kB

Release files / posmatch-0.8.1.tar.gz

Download URL posmatch-0.8.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
37ad6b1f911717c90a69339158bd0d623c8fc298ff0d0244286fa2b1f0ff0b9b
BLAKE2b-256 checksum
How to use checksums
a9c8c97c2f4ef2e339e297aad8c583a97c08b053df740ffc2d830f2722b1a937
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-35-generic

Release files / posmatch-0.8.1-py3-none-any.whl

Download URL posmatch-0.8.1-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b57972e274cdc0f1c6848d797bf71c0b25510ebf36780fb1118bb0d92b6a0610
BLAKE2b-256 checksum
How to use checksums
ceb6c9c879b83c7ab95f972405d4c81da591458c4ae161db2851d87bdc3dfbbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-35-generic

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page