class TagObj: "class presenting tag related methods" # def tagInsert(self, tag): if self.tagList.count(tag) == 0: # if doesn't exists, self.tagList.append(tag) # insert it! # def tagRemove(self, tag): if self.tagList.count(tag) > 0: # if exists, self.tagList.remove(tag) # remove it! # # Returns true (i.e., one) if object has tag # def tagHas(self, tag): return self.tagList.count(tag) # # Returns tag list # def tagCollection(self): return self.tagList # # Returns all objects from objList with same tags as object # def tagSimilar(self, objList): newList = [] for obj in objList: missedTag = 0 for tag in obj.tagList: if self.tagList.count(tag) == 0: missedTag = 1 break if missedTag == 0 and obj.tagList != []: newList.append(obj) return newList # # Returns all objects from objList with at least one tag as object # def tagRelated(self, objList): newList = [] for obj in objList: hasTag = 0 for tag in obj.tagList: if self.tagList.count(tag) > 0: hasTag = 1 break if hasTag == 1: newList.append(obj) return newList # # Returns all tags that object has, that are related to # at least one other object from objList # def tagListAg(self, objList): newList = [] for obj in objList: for tag in obj.tagList: if self.tagList.count(tag) > 0 and \ newList.count(tag) == 0: newList.append(tag) return newList # def __init__(self): self.tagList = [] ############################################# x = TagObj() y = TagObj() z = TagObj() x.tagInsert('old') x.tagInsert('solid') x.tagInsert('light') y.tagInsert('new') y.tagInsert('solid') z.tagInsert('light') print '='*40 print x.tagCollection() print y.tagCollection() print '-'*40 print x.tagSimilar([x,y,z]) print x.tagRelated([y]) print '-'*40 if x.tagHas('old'): print x.tagListAg([y,z])