Metadata-Version: 2.1
Name: posmatch
Version: 0.4.0
Summary: Positional subpattern matching for custom classes.
Home-page: https://github.com/mportesdev/posmatch
Author: Michal Porteš
Author-email: michalportes1@gmail.com
License: MIT License
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Operating System :: OS Independent
Classifier: License :: OSI Approved :: MIT License
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Description-Content-Type: text/x-rst
License-File: LICENSE

Positional subpattern matching for custom classes.

**Note:** This is mostly a toy project. Using it will save you one line
of boiler-plate code at the cost of an additional decorator or
argument. In many cases, the same effect can be easily achieved by
using a dataclass.

Requirements
============

Python 3.8 or higher.

**Note:** Although this package itself does not require Python 3.10,
its usage only makes sense with the new pattern matching feature
introduced in Python 3.10.

Installation
============

.. code::

    pip install posmatch

Usage
=====

The ``pos_match`` decorator
---------------------------

.. code-block:: python

    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(0, 0, b):
            print('shade of blue')
        case Color(r, g, b) if r == g == b:
            print('shade of grey')
        case _:
            print('other color')

Output:

.. code::

    shade of grey

The ``PosMatchMeta`` metaclass
------------------------------

.. code-block:: python

    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 Color(r, g, b) if r == g == b:
            print('shade of grey')
        case _:
            print('other color')

Output:

.. code::

    shade of blue


