Skip to content

Instantly share code, notes, and snippets.

@spumer
Last active August 21, 2021 11:38
Show Gist options
  • Save spumer/49cecb8ed19631e395c4ea0f7575583f to your computer and use it in GitHub Desktop.
Save spumer/49cecb8ed19631e395c4ea0f7575583f to your computer and use it in GitHub Desktop.
ReStr - allow you compare string with regular expression (helpful for testing)
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