本文整理汇总了Python中lru.LRU.keys方法的典型用法代码示例。如果您正苦于以下问题:Python LRU.keys方法的具体用法?Python LRU.keys怎么用?Python LRU.keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lru.LRU
的用法示例。
在下文中一共展示了LRU.keys方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
class topic4:
def __init__(self, c_hash, c_user, c_words):
self.topic_count =1
self.l1 = LRU(c_hash)
self.l2 = LRU(c_user)
def set_hashLRU(self,l):
self.set(self.l1, l)
def set_userLRU(self,l):
self.set(self.l2, l)
def set(self, lru, l):
for k in l:
v = lru.get(k,0)
lru[k]=v+1
def set_cluster(self, hashtags, users, words):
for k in hashtags:
self.l1[k]=self.l1.get(k,0)+1
for k in users:
self.l2[k]=self.l2.get(k,0)+1
self.topic_count+=1
def get_similarity(self,hashtags,users,words):
h_sum = 1
u_sum = 1
w_sum = 1
h_match =0
h_ind =0
u_ind =0
w_ind =0
c=0
h1 = self.l1.get_size()
u1 = self.l2.get_size()
for h in hashtags:
# l1_items=zip(*self.l1.items())
h_sum+= self.l1.get(h,0)
if(self.l1.has_key(h)):
ind = self.l1.keys().index(h)
h_ind+= h1 - ind
h_match+= 1 if ind<250 else 0
for u in users:
u_sum+= self.l2.get(u,0)
if(self.l2.has_key(u)):
u_ind+= u1 - self.l2.keys().index(u)
if(h_match !=0):
c = h_match -1
# print(h_ind,h1,u_ind,u1,w_ind,w1, h_sum,w_sum,)
similarity = (h_ind/(h1+1))*(h_sum/sum(self.l1.values() +[1])) + (u_ind/(u1+1))*(u_sum/sum(self.l2.values()+[1])) +c
return similarity
示例2: test_lru
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
def test_lru(self):
l = LRU(1)
l['a'] = 1
l['a']
self.assertEqual(l.keys(), ['a'])
l['b'] = 2
self.assertEqual(l.keys(), ['b'])
l = LRU(2)
l['a'] = 1
l['b'] = 2
self.assertEqual(len(l), 2)
l['a'] # Testing the first one
l['c'] = 3
self.assertEqual(sorted(l.keys()), ['a', 'c'])
l['c']
self.assertEqual(sorted(l.keys()), ['a', 'c'])
l = LRU(3)
l['a'] = 1
l['b'] = 2
l['c'] = 3
self.assertEqual(len(l), 3)
l['b'] # Testing the middle one
l['d'] = 4
self.assertEqual(sorted(l.keys()), ['b', 'c', 'd'])
l['d'] # Testing the last one
self.assertEqual(sorted(l.keys()), ['b', 'c', 'd'])
l['e'] = 5
self.assertEqual(sorted(l.keys()), ['b', 'd', 'e'])
示例3: test_lru
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
def test_lru(self):
l = LRU(1)
l["a"] = 1
l["a"]
self.assertEqual(l.keys(), ["a"])
l["b"] = 2
self.assertEqual(l.keys(), ["b"])
l = LRU(2)
l["a"] = 1
l["b"] = 2
self.assertEqual(len(l), 2)
l["a"] # Testing the first one
l["c"] = 3
self.assertEqual(sorted(l.keys()), ["a", "c"])
l["c"]
self.assertEqual(sorted(l.keys()), ["a", "c"])
l = LRU(3)
l["a"] = 1
l["b"] = 2
l["c"] = 3
self.assertEqual(len(l), 3)
l["b"] # Testing the middle one
l["d"] = 4
self.assertEqual(sorted(l.keys()), ["b", "c", "d"])
l["d"] # Testing the last one
self.assertEqual(sorted(l.keys()), ["b", "c", "d"])
l["e"] = 5
self.assertEqual(sorted(l.keys()), ["b", "d", "e"])
示例4: test_callback
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
def test_callback(self):
counter = [0]
first_key = 'a'
first_value = 1
def callback(key, value):
self.assertEqual(key, first_key)
self.assertEqual(value, first_value)
counter[0] += 1
l = LRU(1, callback=callback)
l[first_key] = first_value
l['b'] = 1 # test calling the callback
self.assertEqual(counter[0], 1)
self.assertEqual(l.keys(), ['b'])
l['b'] = 2 # doesn't call callback
self.assertEqual(counter[0], 1)
self.assertEqual(l.keys(), ['b'])
self.assertEqual(l.values(), [2])
l = LRU(1, callback=callback)
l[first_key] = first_value
l.set_callback(None)
l['c'] = 1 # doesn't call callback
self.assertEqual(counter[0], 1)
self.assertEqual(l.keys(), ['c'])
l.set_callback(callback)
del l['c'] # doesn't call callback
self.assertEqual(l.keys(), [])
示例5: test_empty
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
def test_empty(self):
l = LRU(1)
self.assertEquals([], l.keys())
self.assertEquals([], l.values())
示例6: Manager
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
#.........这里部分代码省略.........
Sends the content listing for a given path. This detects if the path is
scan, section or fov.
'''
views = []
path_type = self.check_path_type(data_path)
# detect if this is a scan, section or fov
if path_type == 'FOV':
views.append({'data_path': data_path})
elif path_type == 'SECTION':
views.append({'data_path': data_path})
elif path_type == 'SCAN':
scan = Scan.from_directory(data_path, False) # lazy indexing
for i, section in enumerate(scan._sections):
views.append(
{'data_path': os.path.join(data_path, section.id)})
return views
def get_meta_info(self, data_path):
'''
Get meta information for a requested data path.
'''
if data_path not in self._views.keys():
path_type = self.check_path_type(data_path)
# detect if this is a section or fov
if path_type == 'FOV':
# this is a FoV
fov = FoV.from_directory(data_path, True)
view = View.create(
data_path,
[fov],
fov._width,
fov._height,
fov._tx,
fov._ty,
self)
elif path_type == 'SECTION':
section = Section.from_directory(data_path, True, True)
view = View.create(
data_path,
section._fovs,
section._width,
section._height,
section._tx,
section._ty,
self,
section._luts64_map)
#
示例7: __init__
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
class topic4:
def __init__(self, c_hash, c_user, c_words):
self.topic_count =1
# self.time = (self.first,self.last)
self.l1 = LRU(c_hash)
self.first =""
self.last=""
self.lats=[]
self.longs=[]
self.l2 = LRU(c_user)
self.l3 = LRU(c_words)
self.l4 = LRU(400)
def set_hashLRU(self,l):
self.set(self.l1, l)
def set_userLRU(self,l):
self.set(self.l2, l)
def set_wordLRU(self,l):
self.set(self.l3, l)
def set(self, lru, l):
for k in l:
v = lru.get(k,0)
lru[k]=v+1
def set_cluster(self, hashtags, users, words,links, cords):
for k in hashtags:
self.l1[k]=self.l1.get(k,0)+1
for k in users:
self.l2[k]=self.l2.get(k,0)+1
for k in words:
self.l3[k]=self.l3.get(k,0)+1
for k in links:
self.l4[k]=self.l4.get(k,0)+1
if(cords is not None):
self.lats.append(cords["coordinates"][1])
self.longs.append(cords["coordinates"][0])
self.topic_count+=1
def get_similarity(self,hashtags,users,words):
h_sum = 1
u_sum = 1
w_sum = 1
h_match =0
h_ind =0
u_ind =0
w_ind =0
c=0
h1 = self.l1.get_size()
u1 = self.l2.get_size()
w1 = self.l3.get_size()
for h in hashtags:
# l1_items=zip(*self.l1.items())
h_sum+= self.l1.get(h,0)
if(self.l1.has_key(h)):
ind = self.l1.keys().index(h)
h_ind+= h1 - ind
h_match+= 1 if ind<250 else 0
for u in users:
u_sum+= self.l2.get(u,0)
if(self.l2.has_key(u)):
u_ind+= u1 - self.l2.keys().index(u)
for w in words:
w_sum+= self.l3.get(w,0)
if(self.l3.has_key(w)):
w_ind+= w1 - self.l3.keys().index(w)
if(h_match !=0):
c = h_match -1
# print(h_ind,h1,u_ind,u1,w_ind,w1, h_sum,w_sum,)
similarity = (h_ind/(h1+1))*(h_sum/sum(self.l1.values() +[1])) + (u_ind/(u1+1))*(u_sum/sum(self.l2.values()+[1])) + (w_ind/(w1+1))*(w_sum/sum(self.l3.values()+[1])) +c
return similarity
def flush1(self, cache, size):
if(len(cache.keys())>5):
tokens = reversed(cache.keys()[5])
cache.clear()
for i in tokens:
cache[i]=1
def flush(self):
self.flush1(self.l1,500)
self.flush1(self.l2, 500)
self.flush1(self.l3,3500)
self.topic_count=1
示例8: FCP
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import keys [as 别名]
#.........这里部分代码省略.........
while self.treewalk.flist.qsize > 0:
fitems, _ = self.treewalk.flist.mget(G.DB_BUFSIZE)
for fi in fitems:
self.handle_fitem(fi)
self.treewalk.flist.mdel(G.DB_BUFSIZE)
# store checkpoint
log.debug("dbname = %s" % self.circle.dbname)
dirname = os.path.dirname(self.circle.dbname)
basename = os.path.basename(self.circle.dbname)
chkpointname = basename + ".CHECK_OK"
self.checkpoint_file = os.path.join(dirname, chkpointname)
with open(self.checkpoint_file, "w") as f:
f.write("%s" % self.totalsize)
else: # use memory
for fi in self.treewalk.flist:
self.handle_fitem(fi)
# memory-checkpoint
if self.checkpoint_file:
self.do_no_interrupt_checkpoint()
self.checkpoint_last = MPI.Wtime()
def do_open(self, k, d, flag, limit):
"""
@param k: the file path
@param d: dictionary of <path, file descriptor>
@return: file descriptor
"""
if d.has_key(k):
return d[k]
if len(d.keys()) >= limit:
# over the limit
# clean up the least used
old_k, old_v = d.items()[-1]
try:
os.close(old_v)
except OSError as e:
log.warn("FD for %s not valid when closing" % old_k, extra=self.d)
fd = -1
try:
fd = os.open(k, flag)
except OSError as e:
if e.errno == 28: # no space left
log.error("Critical error: %s, exit!" % e, extra=self.d)
self.circle.exit(0) # should abort
else:
log.error("OSError({0}):{1}, skipping {2}".format(e.errno, e.strerror, k), extra=self.d)
else:
if fd > 0:
d[k] = fd
finally:
return fd
@staticmethod
def do_mkdir(work):
src = work.src
dest = work.dest
if not os.path.exists(dest):
os.makedirs(dest)
def do_copy(self, work):
src = work.src