本文整理汇总了Python中lru.LRU.set_size方法的典型用法代码示例。如果您正苦于以下问题:Python LRU.set_size方法的具体用法?Python LRU.set_size怎么用?Python LRU.set_size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lru.LRU
的用法示例。
在下文中一共展示了LRU.set_size方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_capacity_set
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import set_size [as 别名]
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)
示例2: test_callback
# 需要导入模块: from lru import LRU [as 别名]
# 或者: from lru.LRU import set_size [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(counter[0], 1)
self.assertEqual(l.keys(), [])
l = LRU(2, callback=callback)
l['a'] = 1 # test calling the callback
l['b'] = 2 # test calling the callback
self.assertEqual(counter[0], 1)
self.assertEqual(l.keys(), ['b', 'a'])
l.set_size(1)
self.assertEqual(counter[0], 2) # callback invoked
self.assertEqual(l.keys(), ['b'])