# -*- coding: utf-8 -*- class BinaryRelationItem(object): def __init__(self, item1, item2): self.first = item1 self.second = item2 def get_another(self, item): if item == self.first: return self.second if item == self.second: return self.first class BinaryRelationCollection(object): def __init__(self): self.__m_Dic = {} def update(self, item): if not self.is_exist(item): key = self.create_key(item) self.__m_Dic[key] = item def clear(self): self.__m_Dic.clear() @staticmethod def create_key(item): lis = [item.first, item.second] lis.sort() return '%s@%s' % (lis[0], lis[1]) def is_exist(self, item): key = self.create_key(item) return key in self.__m_Dic def __iter__(self): return iter(self.__m_Dic) from relations import test def dic2obj(d): """Convert Dict to Object. """ top = type('new', (object,), d) seqs = tuple, list, set, frozenset for i, j in d.items(): j0 = j try: j0 = json.loads(j0) except Exception as e: pass if isinstance(j0, dict): setattr(top, i, dic2obj(j0)) elif isinstance(j0, seqs): setattr(top, i, type(j0)(dic2obj(sj) if isinstance(sj, dict) else sj for sj in j0)) else: setattr(top, i, j0) return top def sqldata2objlist(data): '''Convert Sqldata to Object list. ''' return list(map(dic2obj,data)) def get_dicdata(data,dic): """ Request db for data through sql. Convert data to dict. :param sql: :param dic: :return: """ dic_data=[dict(zip(dic,item)) for item in data] return list(dic_data)