本文整理汇总了Python中mx.Tools.sortedby方法的典型用法代码示例。如果您正苦于以下问题:Python Tools.sortedby方法的具体用法?Python Tools.sortedby怎么用?Python Tools.sortedby使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mx.Tools
的用法示例。
在下文中一共展示了Tools.sortedby方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cut
# 需要导入模块: from mx import Tools [as 别名]
# 或者: from mx.Tools import sortedby [as 别名]
def cut(self,
NOM=NOM,DENOM=DENOM):
""" Force a cut of the cache's contents.
This will make room for at least one new entry.
"""
if _debug:
print ' Cutting down cache size...'
cachesize = self.cachesize
# Cut the cache down to the entries in recent get history
newdata = {}
known_key = newdata.has_key
data = self.data
for id in self.get_history[-self.locality:]:
if known_key(id):
continue
try:
newdata[id] = data[id]
except KeyError:
pass
cachesize = len(newdata)
if _debug:
print ' Size after cut to recent history:',cachesize
# Check
if cachesize * NOM >= self.max_cachesize * DENOM:
# Calculate weights
d = {}
weights = _weights
d_get = d.get
for i,id in Tools.irange(self.get_history[-self.locality:]):
if not known_key(id):
continue
d[id] = d_get(id,0) + weights[i]
# Delete all entries left from median
ranking = Tools.sortedby(d.items(),1)
if _debug:
print ' Ranking:',ranking
for id,weight in ranking[:len(d)/2]:
if _debug:
print ' Deleting',id,'with weight =',weight
del newdata[id]
# Check
cachesize = len(newdata)
if cachesize * NOM >= self.max_cachesize * DENOM:
# Ok, so the smart way didn't work...
if _debug:
print ' Did not work, going the hard way...'
newdata.clear()
cachesize = 0
self.data = newdata
self.cachesize = cachesize
self.cuts = self.cuts + 1