Created
April 21, 2021 14:38
-
-
Save mdellavo/181f5946539e6d846cf4a44342b2f2a0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 AnyDict(dict): | |
""" | |
>>> AnyDict(foo=1) == {"foo": 1, "bar": 2} | |
True | |
>>> AnyDict(foo=2) == {"foo": 1, "bar": 2} | |
False | |
""" | |
def __eq__(self, other): | |
return isinstance(other, dict) and dict(other, **self) == other | |
class AnyList(list): | |
""" | |
>>> AnyList([1, 2]) == [1, 2, 3] | |
True | |
>>> AnyList([4, 5, 6]) == [1, 2, 3] | |
False | |
""" | |
def __eq__(self, other): | |
return isinstance(other, (list, tuple)) and all(element in other for element in self) | |
class AnyString(object): | |
""" | |
>>> AnyString() == "asasdfasdfasdf" | |
True | |
>>> AnyString(r"^http://www") == "http://www.example.com" | |
True | |
>>> AnyString("com") == "http://www.foo.com" | |
True | |
>>> AnyString("edu") == "http://www.foo.com" | |
False | |
""" | |
def __init__(self, pattern=r".*"): | |
super(AnyString, self).__init__() | |
self.pattern = pattern | |
def __ne__(self, other): | |
return not self.__eq__(other) | |
def __eq__(self, other): | |
return bool(re.search(self.pattern, other)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment