utils.py 903 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # -*- coding: utf-8 -*-
  2. class BinaryRelationItem(object):
  3. def __init__(self, item1, item2):
  4. self.first = item1
  5. self.second = item2
  6. def get_another(self, item):
  7. if item == self.first:
  8. return self.second
  9. if item == self.second:
  10. return self.first
  11. class BinaryRelationCollection(object):
  12. def __init__(self):
  13. self.__m_Dic = {}
  14. def update(self, item):
  15. if not self.is_exist(item):
  16. key = self.create_key(item)
  17. self.__m_Dic[key] = item
  18. def clear(self):
  19. self.__m_Dic.clear()
  20. @staticmethod
  21. def create_key(item):
  22. lis = [item.first, item.second]
  23. lis.sort()
  24. return '%s@%s' % (lis[0], lis[1])
  25. def is_exist(self, item):
  26. key = self.create_key(item)
  27. return key in self.__m_Dic
  28. def __iter__(self):
  29. return iter(self.__m_Dic)