Last active
August 21, 2021 11:38
-
-
Save spumer/49cecb8ed19631e395c4ea0f7575583f to your computer and use it in GitHub Desktop.
ReStr - allow you compare string with regular expression (helpful for testing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
class ReStr: | |
""" | |
>>> ReStr(r'\d+') == '0000000' | |
True | |
>>> ReStr(r'\d.\ds remain') == '0.0s remain' | |
""" # noqa: W605 | |
def __init__(self, patt): | |
self.rexp = re.compile(patt) | |
def __eq__(self, other): | |
if isinstance(other, ReStr): | |
return self.rexp == other.rexp | |
if isinstance(other, str): | |
return self.rexp.search(other) is not None | |
return super().__eq__(other) | |
def __repr__(self): | |
return f'ReStr({self.rexp.pattern})' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment