for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
import keyword
DISCARD = object()
class Pattern(tuple):
"""A matcher that binds a value to a name."""
__slots__ = ()
def __new__(cls, name: str):
if name == '_':
return DISCARD
if not name.isidentifier():
raise ValueError
if keyword.iskeyword(name):
return super().__new__(cls, (name,))
@property
def name(self):
"""Return the name of the matcher."""
return self[0]
def __matmul__(self, other):
return AsPattern(self, other)
class AsPattern(tuple):
"""A matcher that contains further bindings."""
def __new__(cls, matcher: Pattern, match):
if match is DISCARD:
return matcher
return super().__new__(cls, (matcher, match))
def matcher(self):
"""Return the left-hand-side of the as-match."""
def match(self):
"""Return the right-hand-side of the as-match."""
return self[1]
class AttrPattern(tuple):
def __new__(cls, match_dict):
return super().__new__(cls, (tuple(match_dict.items()),))
def match_dict(self):
class DictPattern(tuple):
def __new__(cls, match_dict, *, exhaustive=False):
return super().__new__(cls, (tuple(match_dict.items()), exhaustive))
def exhaustive(self):