You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB

4 years ago
  1. from collections import MutableSet
  2. class SetMixin(MutableSet):
  3. """
  4. Mix-in for sets. You must define __iter__, add, remove
  5. """
  6. def __len__(self):
  7. length = 0
  8. for item in self:
  9. length += 1
  10. return length
  11. def __contains__(self, item):
  12. for has_item in self:
  13. if item == has_item:
  14. return True
  15. return False
  16. issubset = MutableSet.__le__
  17. issuperset = MutableSet.__ge__
  18. union = MutableSet.__or__
  19. intersection = MutableSet.__and__
  20. difference = MutableSet.__sub__
  21. symmetric_difference = MutableSet.__xor__
  22. def copy(self):
  23. return set(self)
  24. def update(self, other):
  25. self |= other
  26. def intersection_update(self, other):
  27. self &= other
  28. def difference_update(self, other):
  29. self -= other
  30. def symmetric_difference_update(self, other):
  31. self ^= other
  32. def discard(self, item):
  33. try:
  34. self.remove(item)
  35. except KeyError:
  36. pass
  37. @classmethod
  38. def _from_iterable(cls, it):
  39. return set(it)