This is a very crude proof of concept that can perform custom match logics in a match
statement.
Usage example:
match Matcher('Hello, Python!'):
case StartsWith('Goodbye'):
print('Farewell')
case Search(r'Hello, (.*)!') as m:
print(f'Greetings to {m.match[1]}')
which prints:
Greetings to Python
The basic idea is to make the attribute specified by __match_args__
a dynamic property that returns an object with a custom equality test that performs the desired match logics.
import re
class MatcherBase:
def __init__(self, string):
self.string = string
def __init_subclass__(cls):
@property
class matcher:
__eq__ = cls.__eq__
def __init__(self, instance):
self.instance = instance
setattr(cls, cls.__name__, matcher)
cls.__match_args__ = cls.__name__,
class StartsWith(MatcherBase):
def __eq__(self, pattern):
return self.instance.string.startswith(pattern)
class Search(MatcherBase):
def __eq__(self, pattern):
self.instance.match = match = re.search(pattern, self.instance.string)
return match
class Matcher(StartsWith, Search):
pass
It still needs features such as support for regex flags, but for easier understanding of the core idea I’ll leave it as is for now.