|release| |pypi|
========
posmatch
========
Positional sub-pattern matching for custom classes.
Requirements
============
Python 3.10 or higher.
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, 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:
.. code::
Shade of blue
The ``PosMatchMeta`` metaclass
------------------------------
.. code-block:: python
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:
.. code::
Distant future
The ``PosMatchMixin`` mix-in class
----------------------------------
.. code-block:: python
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:
.. code::
Square
.. |release| image:: https://img.shields.io/github/v/release/mportesdev/posmatch
:target: https://github.com/mportesdev/posmatch/releases/latest
.. |pypi| image:: https://img.shields.io/pypi/v/posmatch
:target: https://pypi.org/project/posmatch
Raw data
{
"_id": null,
"home_page": "https://github.com/mportesdev/posmatch",
"name": "posmatch",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Michal Porte\u0161",
"author_email": "michalportes1@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a3/3b/8da150de5391eba9c6c85625432cf4e2384b99d23e579d86249ca8afb32e/posmatch-0.8.0.tar.gz",
"platform": null,
"description": "|release| |pypi|\n\n========\nposmatch\n========\n\nPositional sub-pattern matching for custom classes.\n\nRequirements\n============\n\nPython 3.10 or higher.\n\nInstallation\n============\n\n.. code::\n\n pip install posmatch\n\nUsage\n=====\n\nThe ``pos_match`` decorator\n---------------------------\n\n.. code-block:: python\n\n from posmatch import pos_match\n\n\n @pos_match\n class Color:\n def __init__(self, red, green, blue):\n self.red = red\n self.green = green\n self.blue = blue\n\n\n color = Color(0, 0, 128)\n\n match color:\n case Color(r, g, b) if r == g == b:\n print('Shade of grey')\n case Color(0, 0):\n print('Shade of blue')\n\nOutput:\n\n.. code::\n\n Shade of blue\n\nThe ``PosMatchMeta`` metaclass\n------------------------------\n\n.. code-block:: python\n\n from posmatch import PosMatchMeta\n\n\n class Date(metaclass=PosMatchMeta):\n def __init__(self, year, month, day):\n self.year = year\n self.month = month\n self.day = day\n\n\n date = Date(2121, 1, 1)\n\n match date:\n case Date(_, m, d) if m == 5 and d == 1:\n print('May Day')\n case Date(y) if y > 2100:\n print('Distant future')\n\nOutput:\n\n.. code::\n\n Distant future\n\nThe ``PosMatchMixin`` mix-in class\n----------------------------------\n\n.. code-block:: python\n\n from posmatch import PosMatchMixin\n\n\n class Rectangle(PosMatchMixin):\n def __init__(self, width, height):\n self.width = width\n self.height = height\n\n\n shape = Rectangle(16, 16)\n\n match shape:\n case Rectangle(w, h) if w == h:\n print('Square')\n case Rectangle(x, y) if x > y:\n print('Landscape')\n\nOutput:\n\n.. code::\n\n Square\n\n\n.. |release| image:: https://img.shields.io/github/v/release/mportesdev/posmatch\n :target: https://github.com/mportesdev/posmatch/releases/latest\n.. |pypi| image:: https://img.shields.io/pypi/v/posmatch\n :target: https://pypi.org/project/posmatch\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Positional sub-pattern matching for custom classes.",
"version": "0.8.0",
"project_urls": {
"Homepage": "https://github.com/mportesdev/posmatch"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f2cbddf5307cde90c28d0b9139927fc1ee5894a6d340eca21544d0d7e4c51ee5",
"md5": "0319f0b237446d7aec7d5596f2e42109",
"sha256": "4c872ba5740e9e383210dd0c95211ef77b30fac29d5fae9cf642a00c46a41fbe"
},
"downloads": -1,
"filename": "posmatch-0.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0319f0b237446d7aec7d5596f2e42109",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 3649,
"upload_time": "2024-06-12T09:24:17",
"upload_time_iso_8601": "2024-06-12T09:24:17.403430Z",
"url": "https://files.pythonhosted.org/packages/f2/cb/ddf5307cde90c28d0b9139927fc1ee5894a6d340eca21544d0d7e4c51ee5/posmatch-0.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a33b8da150de5391eba9c6c85625432cf4e2384b99d23e579d86249ca8afb32e",
"md5": "fd3b305b11518bf8d1299fa067d2f079",
"sha256": "87832b7cf3b001a74aa5c8f57e01fe7eee3081552fd631e8bfea3fe8bd72ca92"
},
"downloads": -1,
"filename": "posmatch-0.8.0.tar.gz",
"has_sig": false,
"md5_digest": "fd3b305b11518bf8d1299fa067d2f079",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 2999,
"upload_time": "2024-06-12T09:24:18",
"upload_time_iso_8601": "2024-06-12T09:24:18.732680Z",
"url": "https://files.pythonhosted.org/packages/a3/3b/8da150de5391eba9c6c85625432cf4e2384b99d23e579d86249ca8afb32e/posmatch-0.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-12 09:24:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mportesdev",
"github_project": "posmatch",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "posmatch"
}