Skip to content

Instantly share code, notes, and snippets.

@jcdyer
Created November 16, 2015 16:33
Show Gist options
  • Save jcdyer/c7a234c0f54d437e0b85 to your computer and use it in GitHub Desktop.
Save jcdyer/c7a234c0f54d437e0b85 to your computer and use it in GitHub Desktop.
class UniversalSet(set):
def __init__(self):
super(UniversalSet, self).__init__([])
def __and__(self, other):
if not isinstance(other, set):
other = set(other)
return other
def __rand__(self, other):
if not isinstance(other, set):
other = set(other)
return set(other)
def __or__(self, other):
return self
def __ror__(self, other):
return self
def __contains__(self, other):
return True
def __str__(self):
return '{all}'
def intersection(self, other):
return self.__and__(other)
def union(self, other):
return self.__or__(other)
uset = UniversalSet()
def test_uset_membership():
assert 4 in uset
assert 'a' in uset
assert float('nan') in uset
assert None in uset
assert uset in uset
def test_uset_intersection():
assert uset & {1, 3, 5} == {1, 3, 5}
assert {1, 2, 3} & uset == {1, 2, 3}
assert uset & uset == uset
assert uset.intersection({0, 2, 4}) == {0, 2, 4}
def test_reverse_intersection_FAILS():
assert {0, 2, 4}.intersection(uset) == {0, 2, 4}
def test_uset_union():
assert uset | {1, 3, 5} == uset
assert {1, 3, 5} | uset == uset
assert uset | set() == uset
assert uset | uset == uset
assert uset.union({0, 2, 4}) == uset
def test_reverse_union_FAILS():
assert {0, 2, 4}.union(uset) == uset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment