Disregard the fact that I'm using a class as if it were a singleton. ``` class Tag(dict): _byName = {} _byTagId = {} def __init__(self, **kwargs): super().__init__(**kwargs) Tag._byName[kwargs["name"]] = self Tag._byTagId[kwargs["tagId"]] = self self.implications = TagSet([self]) self._implications_collapsed = False def collapseImplications(self): if self._implications_collapsed: return self._implications_collapsed = True for tagT in list(self.implications): tagT.collapseImplications() self.implications.update(tagT.implications) def __hash__(self): return self["tagId"] @classmethod def fromName(cls, name): return Tag._byName[name] @classmethod def fromTagId(cls, tagId): return Tag._byTagId[tagId] @classmethod def allTags(self): return list(Tag._byName.values()) @classmethod def addEquivalence(cls, s, t): tagS = Tag.fromTagId(s) tagT = Tag.fromTagId(t) combined = TagSet() combined.update(tagS.implications) combined.update(tagT.implications) tagS.implications = combined tagT.implications = combined @classmethod def addImplication(cls, s, t): tagS = Tag.fromTagId(s) tagT = Tag.fromTagId(t) tagS.implications.add(tagT) def loadTags(cursor): cursor.execute("select id, name, description, image_count from public.tags") for tagId, name, description, imageCount in cursor.fetchall(): Tag(tagId=tagId, name=name, description=description, imageCount=imageCount) cursor.execute("select tag_id, target_tag_id from public.tag_aliases") for s, t in cursor.fetchall(): Tag.addEquivalence(s, t) cursor.execute("select tag_id, target_tag_id from public.tag_implications") for s, t in cursor.fetchall(): Tag.addImplication(s, t) for tag in Tag.allTags(): tag.collapseImplications() tag['tagsetId'] = tag.implications.tagsetId return Tag._byTagId def loadImageTaggings(cursor, images, tags): result = set() collectedTagsets = set() cursor.execute("select image_id, tag_id from public.image_taggings") for imageId, tagId in cursor.fetchall(): if imageId not in images: continue image = Image.fromImageId(imageId) tag = tags[tagId] result.add((image['imageId'], tag['tagsetId'])) return result