Metadata-Version: 2.1
Name: posmatch
Version: 0.3.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

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
------------

.. code::

    pip install posmatch

Usage
-----

Example 1
~~~~~~~~~

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

Result:

.. code::

    shade of grey

Example 2
~~~~~~~~~

Using 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 _:
            print('other color')

Result:

.. code::

    shade of blue

Example 3
~~~~~~~~~

Using the ``PosMatchMixin`` mix-in class.

.. code-block:: python

    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:

.. code::

    shade of red


