
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
pymatched
Advanced tools
pymatched is a library which provides functional pattern matching.
pip install pymatched
result = match(<'func'>) >> Mapping[Hashable, Any]
as you know, mutable things cannot be key of dict so we can not match easly.
this is the example of list.
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
}
x = match([1, 2, 3]) >> {
list: match(...) >> {
(list, lambda v: v == [1, _, 3]): "pattern is (1, * ,3)",
... : "default"
}
}
from pymatched import match
match(1) >> {
1: "It's 1",
5: "It's 5",
}
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",
}
from pymatched import match
match(42) >> {
int: "int caught",
...: lambda v: f"{type(v)} caught"
}
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",
}
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",
}
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",
}
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)",
}
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"
},
}
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)"
}
FAQs
A library which provides simple functional pattern matching.
We found that pymatched demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.