本文整理汇总了Python中lru.LRU类的典型用法代码示例。如果您正苦于以下问题:Python LRU类的具体用法?Python LRU怎么用?Python LRU使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LRU类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_lru
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"])
示例2: test_lru
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: __init__
def __init__(self):
'''
'''
self._views = LRU(50)
# tile cache - enough for 1 MFOV for 10 parallel users
self._tiles = LRU(61 * 10)
self._client_tiles = {}
示例4: test_access_within_size
def test_access_within_size(self):
for size in SIZES:
l = LRU(size)
for i in xrange(size):
l[i] = str(i)
for i in xrange(size):
self.assertEquals(l[i], str(i))
self.assertEquals(l.get(i,None), str(i))
示例5: test_has_key
def test_has_key(self):
for size in SIZES:
l = LRU(size)
for i in xrange(2*size):
l[i] = str(i)
self.assertTrue(l.has_key(i))
for i in xrange(size, 2*size):
self.assertTrue(l.has_key(i))
for i in xrange(size):
self.assertFalse(l.has_key(i))
示例6: test_get_and_del
def test_get_and_del(self):
l = LRU(2)
l[1] = '1'
self.assertEqual('1', l.get(1))
self.assertEqual('1', l.get(2, '1'))
self.assertIsNone(l.get(2))
self.assertEqual('1', l[1])
self.assertRaises(KeyError, lambda: l['2'])
with self.assertRaises(KeyError):
del l['2']
示例7: test_access
def test_access(self):
for size in SIZES:
l = LRU(size)
n = size * 2
for i in xrange(n):
l[i] = str(i)
self._check_kvi(range(n-1,size-1,-1), l)
for i in xrange(size, n):
self.assertEquals(l[i], str(i))
self.assertEquals(l.get(i,None), str(i))
示例8: __init__
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)
示例9: test_clear
def test_clear(self):
for size in SIZES:
l = LRU(size)
for i in range(size+5):
l[i] = str(i)
l.clear()
for i in range(size):
l[i] = str(i)
for i in xrange(size):
_ = l[random.randint(0, size-1)]
l.clear()
self.assertTrue(len(l) == 0)
示例10: __init__
def __init__(self, mtp, pstorage=None):
global PATH_CACHE_SIZE
MTPRefresh.__init__(self)
self.mtp = mtp
self.libmtp = mtp.libmtp
self.open_device = mtp.open_device
self.directories = None
self.contents = LRU(PATH_CACHE_SIZE)
if pstorage is None:
MTPEntry.__init__(self, -3, '/')
self.storage = None
self.directories = []
for dirname in self.mtp.get_storage_descriptions():
#def __init__(self, path, id=-2, storageid=-2, folderid=-2, mtp=None, timestamp=0, is_refresh=True):
self.directories.append(MTPFolder(path=dirname, id= -3, storageid= -3, folderid= -2, is_refresh=False))
self.root = None
self.contents[utf8(os.sep)] = self
else:
self.storage = pstorage
storage = pstorage.contents
self.type = storage.StorageType
self.freespace = storage.FreeSpaceInBytes
self.capacity = storage.MaxCapacity
path = os.sep + storage.StorageDescription
MTPEntry.__init__(self, storage.id, path, storageid=None, folderid=0)
self.root = MTPFolder(path=path, id=0, storageid=storage.id, folderid=0, mtp=self.mtp)
self.contents[utf8(path)] = self.root
示例11: __init__
class Drawings:
def __init__(self, max_active):
self.drawings = LRU(max_active)
def get_drawing(self, room):
for (k, v) in self.drawings.items():
logger.debug('{}: {} (len {})'.format(id(self), k , len(v)))
try:
d = self.drawings[room]
except KeyError:
d = self.drawings[room] = [ ]
return d
示例12: __init__
def __init__(self, index=None, polygons=None, polygons_db=None, save_dir=None,
index_filename=None,
polygons_db_path=None,
include_only_properties=None):
if save_dir:
self.save_dir = save_dir
else:
self.save_dir = None
if not index_filename:
index_filename = self.INDEX_FILENAME
self.index_path = os.path.join(save_dir or '.', index_filename)
if not index:
self.create_index(overwrite=True)
else:
self.index = index
if include_only_properties and hasattr(include_only_properties, '__contains__'):
self.include_only_properties = include_only_properties
if not polygons and not self.persistent_polygons:
self.polygons = {}
elif polygons and not self.persistent_polygons:
self.polygons = polygons
elif self.persistent_polygons and self.cache_size > 0:
self.polygons = LRU(self.cache_size)
if polygons:
for key, value in six.iteritems(polygons):
self.polygons[key] = value
self.cache_hits = 0
self.cache_misses = 0
self.get_polygon = self.get_polygon_cached
if not polygons_db_path:
polygons_db_path = os.path.join(save_dir or '.', self.POLYGONS_DB_DIR)
if not polygons_db:
self.polygons_db = LevelDB(polygons_db_path)
else:
self.polygons_db = polygons_db
self.setup()
self.i = 0
示例13: __init__
def __init__(self, mtp, mountpoint, is_debug=False, logger=None):
global VERBOSE
self.mtp = mtp
self.is_debug = is_debug
self.tempdir = tempfile.mkdtemp(prefix='pymtpfs')
if not bool(self.tempdir) or not os.path.exists(self.tempdir):
self.tempdir = tempfile.gettempdir()
self.read_timeout = 2
self.write_timeout = 2
self.openfile_t = namedtuple('openfile', 'handle, path, mtp_path, readonly')
self.openfiles = {}
self.log = logger
self.created = LRU(1000)
if VERBOSE:
print("Mounted %s on %s" % (self.mtp, ))
self.log.info("Mounted %s on %s" % (self.mtp, mountpoint))
示例14: test_capacity_set
def test_capacity_set(self):
for size in SIZES:
l = LRU(size)
for i in range(size+5):
l[i] = str(i)
l.set_size(size+10)
self.assertTrue(size+10 == l.get_size())
self.assertTrue(len(l) == size)
for i in range(size+20):
l[i] = str(i)
self.assertTrue(len(l) == size+10)
l.set_size(size+10-1)
self.assertTrue(len(l) == size+10-1)
示例15: __init__
class topic:
def __init__(self, c_hash, c_user, c_words):
self.topic_count =1
self.l1 = LRU(c_hash)
self.l2 = LRU(c_user)
self.l3 = LRU(c_words)
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):
for k in hashtags:
v = self.l1.get(k,0)
self.l1[k]=v+1
for k in users:
v = self.l2.get(k,0)
self.l2[k]=v+1
for k in words:
v = self.l3.get(k,0)
self.l3[k]=v+1
self.topic_count+=1
def get_similarity(self,hashtags,users,words):
h_sum = 0
u_sum = 0
w_sum = 0
for h in hashtags:
h_sum+= self.l1.get(h,0)
for u in users:
u_sum+= self.l2.get(u,0)
for w in words:
w_sum+= self.l3.get(w,0)
similarity = 0.5*h_sum + 0.2*u_sum + 0.3*w_sum
return similarity