##// END OF EJS Templates
util: add method to peek item in lrucachedict...
Yuya Nishihara -
r40915:0c638ff6 default
parent child Browse files
Show More
@@ -1337,6 +1337,20 b' class lrucachedict(object):'
1337 except KeyError:
1337 except KeyError:
1338 return default
1338 return default
1339
1339
1340 def peek(self, k, default=_notset):
1341 """Get the specified item without moving it to the head
1342
1343 Unlike get(), this doesn't mutate the internal state. But be aware
1344 that it doesn't mean peek() is thread safe.
1345 """
1346 try:
1347 node = self._cache[k]
1348 return node.value
1349 except KeyError:
1350 if default is _notset:
1351 raise
1352 return default
1353
1340 def clear(self):
1354 def clear(self):
1341 n = self._head
1355 n = self._head
1342 while n.key is not _notset:
1356 while n.key is not _notset:
@@ -79,6 +79,21 b' class testlrucachedict(unittest.TestCase'
79 self.assertEqual(d.get('a'), 'va')
79 self.assertEqual(d.get('a'), 'va')
80 self.assertEqual(list(d), ['a', 'c', 'b'])
80 self.assertEqual(list(d), ['a', 'c', 'b'])
81
81
82 def testpeek(self):
83 d = util.lrucachedict(4)
84 d['a'] = 'va'
85 d['b'] = 'vb'
86 d['c'] = 'vc'
87
88 with self.assertRaises(KeyError):
89 d.peek('missing')
90 self.assertEqual(list(d), ['c', 'b', 'a'])
91 self.assertIsNone(d.peek('missing', None))
92 self.assertEqual(list(d), ['c', 'b', 'a'])
93
94 self.assertEqual(d.peek('a'), 'va')
95 self.assertEqual(list(d), ['c', 'b', 'a'])
96
82 def testcopypartial(self):
97 def testcopypartial(self):
83 d = util.lrucachedict(4)
98 d = util.lrucachedict(4)
84 d.insert('a', 'va', cost=4)
99 d.insert('a', 'va', cost=4)
General Comments 0
You need to be logged in to leave comments. Login now