Skip to main content

pymatched⇒

What is pymatched?

pymatched is a library which provides functional pattern matching.

Installation

pip install pymatched

Syntax

result = match(<'func'>) >> Mapping[Hashable, Any]

Match order

  1. exact match
  2. oneof match
  3. placeholder match (if target is immutable iterable)
  4. type match with guard (Contravariant match)
  5. type match (Invariant match)
  6. type match (Contravariant match)
  7. handling default if exists

Usage

Note: How to match mutable value?

as you know, mutable things cannot be key of dict so we can not match easly.

this is the example of list.

Case A: use type guards

x = match([1, 2, 3]) >> {
    list                            : "list",
    oneof([1], [1, 2], [1, 2, 3])   : "[1] | [1, 2] | [1, 2, 3]",
    (list, lambda x: x == [1, 2, 3]): "(list, f(list) -> bool)",
    # [1, 2, 3]: "[1, 2, 3]",  --> list is unhashable so not working
}

Case B: use nested match

x = match([1, 2, 3]) >> {
    list: match(...) >> {
        (list, lambda v: v == [1, _, 3]): "pattern is (1, * ,3)",
        ...                             : "default"
    } 
}

Value match

from pymatched import match

match(1) >> {
    1: "It's 1",
    5: "It's 5",
}

Handling default case

use elipsis ... or typing.Any

if nothing catched but default handler not defined, RuntimeError will be raised.

from typing import Any
from pymatched import match

match(None) >> {
    ...: "default",
    # Any: "also default",
}

Type match

from pymatched import match

match(42) >> {
    int: "int caught",
    ...: lambda v: f"{type(v)} caught"
}

Type match with guard

If tuple's first element is type and second element is lambda, this case will be considered as type match with guard.

from pymatched import match

match(42) >> {
    (int, lambda: v: v == 42): "42 caught",
    int                      : "int except 42",
}

type match with guard can use typing.Any.

from typing import Any
from pymatched import match

match(42) >> {
    (Any, lambda: v: v in (42, "42")): "42 caught",
    int                              : "int except 42",
}

Exception match in type match

pymatched.do wraps executing function. when wrapped function raises error, do catch it and return it as normal return.

from pymatched import match, do

def fx(v):
    raise Exception("Ooops!")

match(do(fx, None)) >> {
    Exception: "exception caught",
    ...      : lambda v: f"{v} caught",
}

Oneof match

from pymatched import oneof, match

fx = lambda x: x

match(fx(5)) >> {
    oneof(1, 2, 3): "one of 1, 2, 3",
    oneof(4, 5, 6): "one of 4, 5, 6",
}

Placeholder match

from pymatched import oneof, match

match((1, 2, 3, 4)) >> {  # change (1, 2, 3, 4) into (100, 2, 3, 4) or (1, 9, 3, 9)
    (1, _, 3, _): "pattern (1, *, 3, *)",
    (_, 2, _, 4): "pattern (*, 2, *, 4)",
}

Nested match

If match with pymatchied._ (PlaceholderTyoe) or ... (Ellipsis), this match will be considered as nested match.

from pymatched import match, _

match(5) >> {
    int: match(_) >> {
        5: "It's 5",
        ...: "default"
    },
}

Mixed match

cases could be mixed, but resolved by designated match order.

from pymatched import oneof, match

v = (1, 2, 3)

x = match(v) >> {
    tuple                         : "Tuple caught",
    (tuple, lambda v: v[-1] == 3) : "last item of tuple is 3",
    (1, _, 3)                     : "pattern is (1, *, 3)".
    oneof((1,), (1, 2), (1, 2, 3)): "one of (1,) | (1, 2) | (1, 2, 3)",
    (1, 2, 3)                     : "(1, 2, 3)"
}

Metadata

Release files for pymatched 0.2.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 pymatched 0.2.1
File Size Uploaded Provenance Actions
pymatched-0.2.1.tar.gz 5.1 kB Without provenance Details for pymatched-0.2.1.tar.gz

Built distribution (wheel)

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

Total release size: 9.9 kB

Release files / pymatched-0.2.1.tar.gz

Download URL pymatched-0.2.1.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3e09270245d7f93f953254420752bf7f08e77d327085f8328ffe0f2a0821df4c
BLAKE2b-256 checksum
How to use checksums
1dbbd2412fe236c7fc54c3181aae5141f59f35692597c03b37f64eff6578960c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0 CPython/3.6.1 Darwin/19.0.0

Release files / pymatched-0.2.1-py3-none-any.whl

Download URL pymatched-0.2.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4793328686476c3aa014eafb160ab1eef9f4a22385a859ab452db67b23c5a6c0
BLAKE2b-256 checksum
How to use checksums
51d89f0dec6e2563534c8cd571122818d6c4859fd31e023586bdbe0492e8814b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0 CPython/3.6.1 Darwin/19.0.0

Security for pymatched 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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