# Path: mako/util.py
# class LRUCache(dict):
# 
#     """A dictionary-like object that stores a limited number of items,
#     discarding lesser used items periodically.
# 
#     this is a rewrite of LRUCache from Myghty to use a periodic timestamp-based
#     paradigm so that synchronization is not really needed.  the size management
#     is inexact.
#     """
# 
#     class _Item:
#         def __init__(self, key, value):
#             self.key = key
#             self.value = value
#             self.timestamp = timeit.default_timer()
# 
#         def __repr__(self):
#             return repr(self.value)
# 
#     def __init__(self, capacity, threshold=0.5):
#         self.capacity = capacity
#         self.threshold = threshold
# 
#     def __getitem__(self, key):
#         item = dict.__getitem__(self, key)
#         item.timestamp = timeit.default_timer()
#         return item.value
# 
#     def values(self):
#         return [i.value for i in dict.values(self)]
# 
#     def setdefault(self, key, value):
#         if key in self:
#             return self[key]
#         self[key] = value
#         return value
# 
#     def __setitem__(self, key, value):
#         item = dict.get(self, key)
#         if item is None:
#             item = self._Item(key, value)
#             dict.__setitem__(self, key, item)
#         else:
#             item.value = value
#         self._manage_size()
# 
#     def _manage_size(self):
#         while len(self) > self.capacity + self.capacity * self.threshold:
#             bytime = sorted(
#                 dict.values(self),
#                 key=operator.attrgetter("timestamp"),
#                 reverse=True,
#             )
#             for item in bytime[self.capacity :]:
#                 try:
#                     del self[item.key]
#                 except KeyError:
#                     # if we couldn't find a key, most likely some other thread
#                     # broke in on us. loop around and try again
#                     break


from mako.util import LRUCache
        self.id = id_

    def __str__(self):
        return "item id %d" % self.id


class LRUTest:
    def testlru(self):
        l = LRUCache(10, threshold=0.2)

        for id_ in range(1, 20):
            l[id_] = item(id_)

        # first couple of items should be gone
        assert 1 not in l
        assert 2 not in l

        # next batch over the threshold of 10 should be present
        for id_ in range(11, 20):
            assert id_ in l

        l[12]
        l[15]
        l[23] = item(23)
        l[24] = item(24)
        l[25] = item(25)
        l[26] = item(26)
        l[27] = item(27)

        assert 11 not in l

# Path: mako/util.py
# class LRUCache(dict):
# 
#     """A dictionary-like object that stores a limited number of items,
#     discarding lesser used items periodically.
# 
#     this is a rewrite of LRUCache from Myghty to use a periodic timestamp-based
#     paradigm so that synchronization is not really needed.  the size management
#     is inexact.
#     """
# 
#     class _Item:
#         def __init__(self, key, value):
#             self.key = key
#             self.value = value
#             self.timestamp = timeit.default_timer()
# 
#         def __repr__(self):
#             return repr(self.value)
# 
#     def __init__(self, capacity, threshold=0.5):
#         self.capacity = capacity
#         self.threshold = threshold
# 
#     def __getitem__(self, key):
#         item = dict.__getitem__(self, key)
#         item.timestamp = timeit.default_timer()
#         return item.value
# 
#     def values(self):
#         return [i.value for i in dict.values(self)]
# 
#     def setdefault(self, key, value):
#         if key in self:
#             return self[key]
#         self[key] = value
#         return value
# 
#     def __setitem__(self, key, value):
#         item = dict.get(self, key)
#         if item is None:
#             item = self._Item(key, value)
#             dict.__setitem__(self, key, item)
#         else:
#             item.value = value
#         self._manage_size()
# 
#     def _manage_size(self):
#         while len(self) > self.capacity + self.capacity * self.threshold:
#             bytime = sorted(
#                 dict.values(self),
#                 key=operator.attrgetter("timestamp"),
#                 reverse=True,
#             )
#             for item in bytime[self.capacity :]:
#                 try:
#                     del self[item.key]
#                 except KeyError:
#                     # if we couldn't find a key, most likely some other thread
#                     # broke in on us. loop around and try again
#                     break


# Path: test/test_lru.py
from mako.util import LRUCache

        self.id = id_

    def __str__(self):
        return "item id %d" % self.id


class LRUTest:
    def testlru(self):
        l = LRUCache(10, threshold=0.2)

        for id_ in range(1, 20):
            l[id_] = item(id_)

        # first couple of items should be gone
        assert 1 not in l
        assert 2 not in l

        # next batch over the threshold of 10 should be present
        for id_ in range(11, 20):
            assert id_ in l

        l[12]
        l[15]
        l[23] = item(23)
        l[24] = item(24)
        l[25] = item(25)
        l[26] = item(26)
        l[27] = item(27)

        assert 11 not in l

        assert 13 not in l