for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
import keyword
from .compound_match import CompoundMatch
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 tuple.__getitem__(self, 0)
def __getitem__(self, other):
return AsPattern(self, other)
class AsPattern(CompoundMatch, tuple):
"""A matcher that contains further bindings."""
def __new__(cls, pattern: Pattern, structure):
if structure is DISCARD:
return pattern
return super().__new__(cls, (pattern, structure))
def pattern(self):
"""Return the left-hand-side of the as-match."""
return self[0]
def structure(self):
"""Return the right-hand-side of the as-match."""
return self[1]
def destructure(self, value):
if isinstance(value, AsPattern):
if value is self:
return (self.structure, self.pattern)
return (value.structure, value)
return (value, value)