Changeset 786:4da2efa4bf8f


Ignore:
Timestamp:
Apr 30, 2009, 11:08:19 PM (12 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Convert:
svn:778c30c8-61e0-0310-89d4-fe2f97a467b2/trunk@837
Message:
Reverted changes which should have gone into the issue32 branch.
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • lrucache.py

    r785 r786  
    3939"""
    4040
     41from __future__ import generators
    4142import time
    42 from ftputil_heapq import heappush, heappop, heapify
     43from heapq import heappush, heappop, heapify
    4344
    4445__version__ = "0.2"
     
    110111            return cmp(self.atime, other.atime)
    111112
     113        def __repr__(self):
     114            return "<%s %s => %s (%s)>" % \
     115                   (self.__class__, self.key, self.obj, \
     116                    time.asctime(time.localtime(self.atime)))
    112117
    113118    def __init__(self, size=DEFAULT_SIZE):
     
    121126        self.__dict = {}
    122127        self.size = size
     128        """Maximum size of the cache.
     129        If more than 'size' elements are added to the cache,
     130        the least-recently-used ones will be discarded."""
    123131
    124132    def __len__(self):
     
    163171            return node.obj
    164172
     173    def __iter__(self):
     174        copy = self.__heap[:]
     175        while len(copy) > 0:
     176            node = heappop(copy)
     177            yield node.key
     178        raise StopIteration
     179
    165180    def __setattr__(self, name, value):
    166181        object.__setattr__(self, name, value)
     
    171186                del self.__dict[lru.key]
    172187
     188    def __repr__(self):
     189        return "<%s (%d elements)>" % (str(self.__class__), len(self.__heap))
     190
    173191    def mtime(self, key):
    174192        """Return the last modification time for the cache record with key.
     
    182200
    183201if __name__ == "__main__":
    184     cache = LRUCache(15)
    185     for i in range(20):
     202    cache = LRUCache(25)
     203    print cache
     204    for i in range(50):
    186205        cache[i] = str(i)
    187     if 16 in cache:
    188         del cache[16]
    189     # traceback occurs in this assignment
    190     cache.size = 5
     206    print cache
     207    if 46 in cache:
     208        del cache[46]
     209    print cache
     210    cache.size = 10
     211    print cache
     212    cache[46] = '46'
     213    print cache
     214    print len(cache)
     215    for c in cache:
     216        print c
     217    print cache
     218    print cache.mtime(46)
     219    for c in cache:
     220        print c
Note: See TracChangeset for help on using the changeset viewer.