def marshal(obj, keys=None, marshallerName='', objs=None): """ Convert an object to a dictionary. keys is an optional list of keys to include in the returned dictionary. if keys is None then all public attributes are returned. marshallerName is an optional marshalling adapter name. if it is an empty string then the default marshaller will be used. """ #to prevent recursing back over something twice, keep track of seen objs if objs is None: objs = OOSet() # obj is itself marshallable, so make a marshaller and marshal away if IMarshallable.providedBy(obj): marshaller = component.getAdapter(obj, IMarshaller, marshallerName) verify.verifyObject(IMarshaller, marshaller) if IInfo.providedBy(obj): key = (getattr(obj._object, '_p_oid', id(obj)), obj.__class__) if key in objs: raise AlreadySeenException() else: objs.insert(key) try: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) except AlreadySeenException: pass finally: objs.remove(key) else: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) # obj is a dict, so marshal its values recursively # Zuul.marshal({'foo':1, 'bar':2}) if isinstance(obj, dict): marshalled_dict = {} for k in obj: try: marshalled_dict[k] = marshal(obj[k], keys, marshallerName, objs) except AlreadySeenException: pass return marshalled_dict # obj is a non-string iterable, so marshal its members recursively # Zuul.marshal(set([o1, o2])) elif hasattr(obj, '__iter__'): marshalled_list = [] for o in obj: try: marshalled_list.append(marshal(o, keys, marshallerName, objs)) except AlreadySeenException: pass return marshalled_list elif isinstance(obj, DateTime ): return str(obj) # Nothing matched, so it's a string or number or other unmarshallable. else: return obj
def marshal(obj, keys=None, marshallerName='', objs=None): """ Convert an object to a dictionary. keys is an optional list of keys to include in the returned dictionary. if keys is None then all public attributes are returned. marshallerName is an optional marshalling adapter name. if it is an empty string then the default marshaller will be used. """ #to prevent recursing back over something twice, keep track of seen objs if objs is None: objs = OOSet() # obj is itself marshallable, so make a marshaller and marshal away if IMarshallable.providedBy(obj): marshaller = component.getAdapter(obj, IMarshaller, marshallerName) verify.verifyObject(IMarshaller, marshaller) if IInfo.providedBy(obj): key = (obj._object._p_oid, obj.__class__) if key in objs: raise AlreadySeenException() else: objs.insert(key) try: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) except AlreadySeenException: pass finally: objs.remove(key) else: return marshal(marshaller.marshal(keys), keys, marshallerName, objs) # obj is a dict, so marshal its values recursively # Zuul.marshal({'foo':1, 'bar':2}) if isinstance(obj, dict): marshalled_dict = {} for k in obj: try: marshalled_dict[k] = marshal(obj[k], keys, marshallerName, objs) except AlreadySeenException: pass return marshalled_dict # obj is a non-string iterable, so marshal its members recursively # Zuul.marshal(set([o1, o2])) elif hasattr(obj, '__iter__'): marshalled_list = [] for o in obj: try: marshalled_list.append(marshal(o, keys, marshallerName, objs)) except AlreadySeenException: pass return marshalled_list elif isinstance(obj, DateTime ): return str(obj) # Nothing matched, so it's a string or number or other unmarshallable. else: return obj
class StarredTasks(grok.Annotation): grok.context(ifaces.IAccount) grok.implements(ifaces.IStarredTasks) def __init__(self): self._starred_tasks = OOSet() def getStarredTasks(self): return list(self._starred_tasks) def addStarredTask(self, task_intid): self._starred_tasks.insert(task_intid) def removeStarredTask(self, task_intid): if task_intid in self._starred_tasks: self._starred_tasks.remove(task_intid)
class LangField(BaseField): """ Language sensitive field. """ def __init__(self, key=None, main_lang = None, **kwargs): super(LangField, self).__init__(key=key, **kwargs) if not main_lang: request = get_current_request() main_lang = request.registry.settings.get('default_locale_name', 'en') self.main_lang = main_lang self.fuzzy = OOSet() self.__data__ = OOBTree() @property def langs(self): return set(self.__data__.keys()) @property def translated(self): return self.langs - set([self.main_lang]) def get(self, default=None, langs = None, **kwargs): if not langs: request = get_current_request() langs = (get_locale_name(request),) return dict((lang, self.__data__.get(lang, default)) for lang in langs) def set(self, value, **kwargs): if not isinstance(value, dict): raise TypeError("Must be a dict") updated = value.keys() main_updated = self.main_lang in updated for (lang, value) in value.items(): self.__data__[lang] = value if lang in self.fuzzy: self.fuzzy.remove(lang) if main_updated: others = self.translated - set(updated) self.fuzzy.update(others) def remove(self, key): self.__data__.pop(key, None) if key in self.fuzzy: self.fuzzy.remove(key) def __len__(self): return len(self.__data__)
class HashList(Content, ContextACLMixin, LocalRolesMixin): type_name = "HashList" type_title = _("HashList") type_description = _( "Encrypted text-rows that can be matched to this document") add_permission = PERM_MANAGE_SYSTEM css_icon = "glyphicon glyphicon-lock" nav_visible = False listing_visible = False search_visible = False salt = None def __init__(self, **kw): self.salt = bcrypt.gensalt() self.hashset = OOSet() self.plaintext_rows = OOSet() super(HashList, self).__init__(**kw) def check(self, value): value = b64encode(value.encode('utf-8')) if value in self.plaintext_rows: return True return bcrypt.hashpw(value, self.salt) in self.hashset def hash_plaintext(self, limit=100): rows = list(self.plaintext_rows)[:limit] for row in rows: hashed = bcrypt.hashpw(row, self.salt) if hashed not in self.hashset: self.hashset.add(hashed) self.plaintext_rows.remove(row) return len(self.plaintext_rows) def hash_and_remove_plaintext(self, limit=100): rows = list(self.plaintext_rows)[:limit] for row in rows: hashed = bcrypt.hashpw(row, self.salt) if hashed in self.hashset: self.hashset.remove(hashed) self.plaintext_rows.remove(row) return len(self.plaintext_rows) @property def plaintext(self): return '' @plaintext.setter def plaintext(self, value): for row in value.splitlines(): row = row.strip() if row and row not in self.plaintext_rows: self.plaintext_rows.add(b64encode(row.encode('utf-8')))
class TaggingEngine(Persistent): interface.implements(ITaggingEngine) def __init__(self): self.clear() @property def tagsCount(self): return len(self.tag_oids) @property def itemsCount(self): return len(self.oid_tags) def clear(self): self.tagsmap = LOBTree() # index self.tag_oids = LOBTree() self.oid_tags = LOBTree() # tag weight self.weights = OOSet() self.tag_weight = LFBTree() def getHash(self, str): if not str: return 0 # empty res = hash(str) # NOTE: workaround for 64bit if struct.calcsize("P") * 8 == 64: res = ord(str[0]) << 7 for char in str: res = c_mul(1000003, res) ^ ord(char) res = res ^ len(str) if res == -1: res = -2 if res >= 2**31: res -= 2**32 return res def update(self, oid, tags): self.remove(oid) for tag in tags: htag = self.getHash(tag) self.tagsmap[htag] = tag # add oid -> tag oids = self.tag_oids.get(htag) if oids is None: oids = LFSet() self.tag_oids[htag] = oids if oid not in oids: oids.insert(oid) # add tag -> oid oid_tags = self.oid_tags.get(oid) if oid_tags is None: oid_tags = LLSet() self.oid_tags[oid] = oid_tags if htag not in oid_tags: oid_tags.insert(htag) # culculate weight weight = self.tag_weight.get(htag) if weight is not None: key = (weight, htag) if key in self.weights: self.weights.remove(key) weight = float(len(oids)) self.tag_weight[htag] = weight self.weights.insert((weight, htag)) def remove(self, oid): for tag in self.oid_tags.get(oid, ()): # remove oid from tag -> oids reference oids = self.tag_oids.get(tag) if oid in oids: oids.remove(oid) oids_len = float(len(oids)) # reculculate weight weight = self.tag_weight.get(tag) if weight is not None: key = (weight, tag) if key in self.weights: self.weights.remove(key) if oids_len: self.tag_weight[tag] = oids_len self.weights.insert((oids_len, tag)) # remove tag if not oids_len: del self.tag_oids[tag] del self.tagsmap[tag] if oid in self.oid_tags: del self.oid_tags[oid] def getItems(self, tags): oids = self.tag_oids weights = self.tag_weight weight, result = 0, LFBTree() for tag in tags: htag = self.getHash(tag) if htag in oids: weight, result = weightedUnion( oids.get(htag), result, weights.get(htag), weight) return IFBucket(result) def getUniqueItems(self, tags): oids = self.tag_oids weights = self.tag_weight weight, result = 1.0, None for tag in tags: htag = self.getHash(tag) if htag in oids: if result is None: weight, result = weightedUnion( oids.get(htag), LFBTree(), weights.get(htag), weight) else: weight, result = weightedIntersection( result, oids.get(htag), weight, weights.get(htag)) return IFBucket(result) def getTagCloud(self, reverse=False): total = len(self.oid_tags) if not total: return tags = self.tagsmap data = self.weights.keys() percent = total / 100.0 if reverse: first = len(data)-1 for idx in xrange(first, -1, -1): weight, htag = data[idx] yield weight / percent, tags.get(htag) else: for weight, htag in data: yield weight / percent, tags.get(htag) def getItemsTagCloud(self, items): oid_tags = self.oid_tags tags = [oid_tags[oid] for oid in items if oid in oid_tags] if tags: tags = multiunion(tags) else: return total = len(oid_tags) data = self.weights.keys() weights = self.tag_weight percent = total / 100.0 for tag in tags: yield weights[tag] / percent, self.tagsmap.get(tag) def getFrequency(self, tags): tagsmap = self.tagsmap tag_weight = self.tag_weight for tag in tags: yield (tag, tag_weight.get(self.getHash(tag), 0)) def __nonzero__(self): return bool(self.tag_oids) def __contains__(self, tag): return self.getHash(tag) in self.tagsmap
class BaseBlog(ContentContainer): interface.implements(IBlog, IBlogPostListing) pageSize = FieldProperty(IBlog["pageSize"]) def __init__(self, *args, **kw): super(BaseBlog, self).__init__(*args, **kw) self.dates = OOSet() self.index = IFBTree() def entries(self, index=None): if index is None: result = self.index else: _t, result = weightedIntersection(index, self.index) return result.byValue(0) def update(self, post): uid = getUtility(IIntIds).getId(post) if uid in self.index: self.remove(post) date = post.date.timetuple() idx = time.mktime(date) self.index[uid] = idx # update in dates yearName = str(date[0]) year = self.get(yearName) if year is None: year = Year() event.notify(ObjectCreatedEvent(year)) self[yearName] = year self.dates.insert(yearName) year.update(date, uid, idx) # update categories self["category"].update(post, uid, idx) # update tags info self["tags"].update(post, uid) def remove(self, post): uid = getUtility(IIntIds).getId(post) if uid not in self.index: return date = time.localtime(self.index[uid]) # remove from dates yearName = str(date[0]) year = self.get(yearName) if year is not None: year.remove(date, uid) if not len(year): if yearName in self.dates: self.dates.remove(yearName) del self[yearName] # remove from category self["category"].remove(uid) # remove tags info self["tags"].remove(post, uid) # remove from index del self.index[uid]
class BudgetGroup(Persistent): """ The BudgetGroup class contains a set of BudgetItem objects and is contained by Project. It has Name and Description string attributes, and a set of BudgetItems. It has methods to add and delete BudgetItem instances from the set. BudgetGroup has functions to calculate the subtotal, vat, and total of the BudgetItems in the set. The class is hashable and has a toString method. """ VAT = 0.14 def __init__(self, name = '', desc = ''): """ The BudgetGroup constructor takes a string name and desc as the Name of the project, and it's Description. It initialises with an empty set. """ self.Name = name self.Description = desc self.ItemSet = OOSet() def add(self, item): """The add method adds a BudgetItem object to the set.""" self.ItemSet.add(item) def delete(self, item): """ The delete method removes a BudgetGroup object from the set. Before removing an object it makes sure it is in the set. """ if (item in self.ItemSet): self.ItemSet.remove(item) return "Confirmed." else: return "Not in set." def subtotal(self): """ The subtotal function returns the total of the subtotal of all the BudgetItem objects. """ stotal = 0 for group in self.ItemSet: stotal+=group.subtotal() return stotal def vat(self): """ The vat function calculates the VAT percentage on the subtotal. Computing the percentage on the subtotal is more efficient than computing the percentage of each item and adding it. """ #return (self.subtotal()*VAT) return (self.subtotal()*0.14) def total (self): """ The total function computes the cost of the BudgetGroup plus VAT. Computing the subtotal increasing it with the VAT percentage is more efficient than computing the subtotal and VAT separately. """ #return (self.subtotal()*(1+VAT)) return (self.subtotal()*(1+0.14)) def __hash__(self): """This enables the class to be hashable""" return hash(self.Name) def __eq__(self, other): """This enables the class to be hashable""" return self.Name == other.Name def __str__(self): """ The toString method returns a string of the name and description of the class. If the set is not empty thereafter it prints all the BudgetItems in the set. """ output = self.Name + ": " + self.Description if self.ItemSet is not None: for item in self.ItemSet: output+=("\n\t\t"+str(item)) return output