Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created April 21, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdellavo/181f5946539e6d846cf4a44342b2f2a0 to your computer and use it in GitHub Desktop.
Save mdellavo/181f5946539e6d846cf4a44342b2f2a0 to your computer and use it in GitHub Desktop.
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