亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Python > 正文

Python實現的最近最少使用算法

2020-01-04 18:05:56
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Python實現的最近最少使用算法,涉及節點、時間、流程控制等相關技巧,需要的朋友可以參考下

本文實例講述了Python實現的最近最少使用算法。分享給大家供大家參考。具體如下:

 

 
  1. # lrucache.py -- a simple LRU (Least-Recently-Used) cache class  
  2. # Copyright 2004 Evan Prodromou <evan@bad.dynu.ca>  
  3. # Licensed under the Academic Free License 2.1  
  4. # Licensed for ftputil under the revised BSD license  
  5. # with permission by the author, Evan Prodromou. Many  
  6. # thanks, Evan! :-)  
  7.  
  8. # The original file is available at  
  9. # http://pypi.python.org/pypi/lrucache/0.2 .  
  10. # arch-tag: LRU cache main module  
  11. """a simple LRU (Least-Recently-Used) cache module  
  12. This module provides very simple LRU (Least-Recently-Used) cache  
  13. functionality.  
  14. An *in-memory cache* is useful for storing the results of an  
  15. 'expe/nsive' process (one that takes a lot of time or resources) for  
  16. later re-use. Typical examples are accessing data from the filesystem,  
  17. a database, or a network location. If you know you'll need to re-read  
  18. the data again, it can help to keep it in a cache.  
  19. You *can* use a Python dictionary as a cache for some purposes.  
  20. However, if the results you're caching are large, or you have a lot of  
  21. possible results, this can be impractical memory-wise.  
  22. An *LRU cache*, on the other hand, only keeps _some_ of the results in  
  23. memory, which keeps you from overusing resources. The cache is bounded  
  24. by a maximum size; if you try to add more values to the cache, it will  
  25. automatically discard the values that you haven't read or written to  
  26. in the longest time. In other words, the least-recently-used items are  
  27. discarded. [1]_  
  28. .. [1]: 'Discarded' here means 'removed from the cache'.  
  29. ""
  30. from __future__ import generators  
  31. import time  
  32. from heapq import heappush, heappop, heapify  
  33. # the suffix after the hyphen denotes modifications by the  
  34. # ftputil project with respect to the original version  
  35. __version__ = "0.2-1" 
  36. __all__ = ['CacheKeyError''LRUCache''DEFAULT_SIZE']  
  37. __docformat__ = 'reStructuredText en' 
  38. DEFAULT_SIZE = 16 
  39. """Default size of a new LRUCache object, if no 'size' argument is given.""" 
  40. class CacheKeyError(KeyError):  
  41. """Error raised when cache requests fail  
  42. When a cache record is accessed which no longer exists (or never did),  
  43. this error is raised. To avoid it, you may want to check for the existence  
  44. of a cache record before reading or deleting it.""
  45. pass 
  46. class LRUCache(object):  
  47. """Least-Recently-Used (LRU) cache.  
  48. Instances of this class provide a least-recently-used (LRU) cache. They  
  49. emulate a Python mapping type. You can use an LRU cache more or less like  
  50. a Python dictionary, with the exception that objects you put into the  
  51. cache may be discarded before you take them out.  
  52. Some example usage::  
  53. cache = LRUCache(32) # new cache  
  54. cache['foo'] = get_file_contents('foo') # or whatever  
  55. if 'foo' in cache: # if it's still in cache...  
  56. # use cached version  
  57. contents = cache['foo']  
  58. else:  
  59. # recalculate  
  60. contents = get_file_contents('foo')  
  61. # store in cache for next time  
  62. cache['foo'] = contents  
  63. print cache.size # Maximum size  
  64. print len(cache) # 0 <= len(cache) <= cache.size  
  65. cache.size = 10 # Auto-shrink on size assignment  
  66. for i in range(50): # note: larger than cache size  
  67. cache[i] = i  
  68. if 0 not in cache: print 'Zero was discarded.'  
  69. if 42 in cache:  
  70. del cache[42] # Manual deletion  
  71. for j in cache: # iterate (in LRU order)  
  72. print j, cache[j] # iterator produces keys, not values  
  73. ""
  74. class __Node(object):  
  75. """Record of a cached value. Not for public consumption.""" 
  76. def __init__(self, key, obj, timestamp, sort_key):  
  77. object.__init__(self)  
  78. self.key = key  
  79. self.obj = obj  
  80. self.atime = timestamp  
  81. self.mtime = self.atime  
  82. self._sort_key = sort_key  
  83. def __cmp__(self, other):  
  84. return cmp(self._sort_key, other._sort_key)  
  85. def __repr__(self):  
  86. return "<%s %s => %s (%s)>" % /  
  87. (self.__class__, self.key, self.obj, /  
  88. time.asctime(time.localtime(self.atime)))  
  89. def __init__(self, size=DEFAULT_SIZE):  
  90. # Check arguments  
  91. if size <= 0:  
  92. raise ValueError, size  
  93. elif type(size) is not type(0):  
  94. raise TypeError, size  
  95. object.__init__(self)  
  96. self.__heap = []  
  97. self.__dict = {}  
  98. """Maximum size of the cache.  
  99. If more than 'size' elements are added to the cache,  
  100. the least-recently-used ones will be discarded.""
  101. self.size = size  
  102. self.__counter = 0 
  103. def _sort_key(self):  
  104. """Return a new integer value upon every call.  
  105. Cache nodes need a monotonically increasing time indicator.  
  106. time.time() and time.clock() don't guarantee this in a  
  107. platform-independent way.  
  108. ""
  109. self.__counter += 1 
  110. return self.__counter  
  111. def __len__(self):  
  112. return len(self.__heap)  
  113. def __contains__(self, key):  
  114. return self.__dict.has_key(key)  
  115. def __setitem__(self, key, obj):  
  116. if self.__dict.has_key(key):  
  117. node = self.__dict[key]  
  118. # update node object in-place  
  119. node.obj = obj  
  120. node.atime = time.time()  
  121. node.mtime = node.atime  
  122. node._sort_key = self._sort_key()  
  123. heapify(self.__heap)  
  124. else:  
  125. # size may have been reset, so we loop  
  126. while len(self.__heap) >= self.size:  
  127. lru = heappop(self.__heap)  
  128. del self.__dict[lru.key]  
  129. node = self.__Node(key, obj, time.time(), self._sort_key())  
  130. self.__dict[key] = node  
  131. heappush(self.__heap, node)  
  132. def __getitem__(self, key):  
  133. if not self.__dict.has_key(key):  
  134. raise CacheKeyError(key)  
  135. else:  
  136. node = self.__dict[key]  
  137. # update node object in-place  
  138. node.atime = time.time()  
  139. node._sort_key = self._sort_key()  
  140. heapify(self.__heap)  
  141. return node.obj  
  142. def __delitem__(self, key):  
  143. if not self.__dict.has_key(key):  
  144. raise CacheKeyError(key)  
  145. else:  
  146. node = self.__dict[key]  
  147. del self.__dict[key]  
  148. self.__heap.remove(node)  
  149. heapify(self.__heap)  
  150. return node.obj  
  151. def __iter__(self):  
  152. copy = self.__heap[:]  
  153. while len(copy) > 0:  
  154. node = heappop(copy)  
  155. yield node.key  
  156. raise StopIteration  
  157. def __setattr__(self, name, value):  
  158. object.__setattr__(self, name, value)  
  159. # automagically shrink heap on resize  
  160. if name == 'size':  
  161. while len(self.__heap) > value:  
  162. lru = heappop(self.__heap)  
  163. del self.__dict[lru.key]  
  164. def __repr__(self):  
  165. return "<%s (%d elements)>" % (str(self.__class__), len(self.__heap))  
  166. def mtime(self, key):  
  167. """Return the last modification time for the cache record with key.  
  168. May be useful for cache instances where the stored values can get  
  169. 'stale', such as caching file or network resource contents.""
  170. if not self.__dict.has_key(key):  
  171. raise CacheKeyError(key)  
  172. else:  
  173. node = self.__dict[key]  
  174. return node.mtime  
  175. if __name__ == "__main__":  
  176. cache = LRUCache(25)  
  177. print cache  
  178. for i in range(50):  
  179. cache[i] = str(i)  
  180. print cache  
  181. if 46 in cache:  
  182. print "46 in cache" 
  183. del cache[46]  
  184. print cache  
  185. cache.size = 10 
  186. print cache  
  187. cache[46] = '46' 
  188. print cache  
  189. print len(cache)  
  190. for c in cache:  
  191. print c  
  192. print cache  
  193. print cache.mtime(46)  
  194. for c in cache:  
  195. print c  

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲欧洲在线视频| 久久99久久99精品中文字幕| 57pao国产成人免费| 欧美性高潮床叫视频| 欧美韩国理论所午夜片917电影| 欧美激情中文网| 欧美另类暴力丝袜| 日韩视频免费在线观看| 51精品国产黑色丝袜高跟鞋| 亚洲自拍偷拍视频| 亚洲成色www8888| 亚洲伊人久久综合| 91在线精品视频| 亚洲国产成人精品一区二区| 有码中文亚洲精品| 一区二区三区精品99久久| 成人黄色免费网站在线观看| 亚洲一区二区少妇| 91欧美精品午夜性色福利在线| 久青草国产97香蕉在线视频| 亚洲人成网站999久久久综合| 在线国产精品播放| 亚洲欧洲自拍偷拍| 日韩欧美在线字幕| 欧美另类99xxxxx| 精品无人国产偷自产在线| 久久影视三级福利片| 久久久久久这里只有精品| 亚洲aa在线观看| 中文字幕欧美亚洲| 日韩欧美中文第一页| 亚洲图片欧美日产| 伊人久久免费视频| 欧美性生交xxxxxdddd| 欧美日韩一区二区在线播放| 欧美激情视频三区| 97国产真实伦对白精彩视频8| 91精品视频在线播放| 欧美日本在线视频中文字字幕| 亚洲精品98久久久久久中文字幕| 国产+成+人+亚洲欧洲| 亚洲成人激情视频| 欧美成人精品激情在线观看| xvideos亚洲人网站| 国产精品久久久久久久av电影| 日韩欧美国产骚| 亚洲自拍另类欧美丝袜| 精品爽片免费看久久| 午夜精品一区二区三区在线播放| 国产亚洲成精品久久| 在线观看欧美日韩国产| 久国内精品在线| 亚洲国产三级网| 国产精品视频久久久久| 一区二区三区国产视频| www.xxxx欧美| 午夜精品久久久久久久99热浪潮| 热久久这里只有| 欧美日韩在线视频一区| 奇米四色中文综合久久| 日韩av免费在线| 久久久精品国产网站| 日韩av免费网站| 中文字幕日韩av电影| 2021久久精品国产99国产精品| 亚洲精品在线91| 亚洲午夜女主播在线直播| 久久久成人精品| 欧美多人乱p欧美4p久久| 精品性高朝久久久久久久| 亚洲人成77777在线观看网| 国产亚洲精品va在线观看| 92看片淫黄大片欧美看国产片| 久久久国产成人精品| 国产精品久久久久久久久免费| www.久久色.com| 欧美精品videos性欧美| 最新国产精品亚洲| 91欧美精品午夜性色福利在线| 国产精品日韩欧美综合| 亚洲欧美综合精品久久成人| 日韩av电影免费观看高清| 亚洲国产精品va在线观看黑人| 亚洲第一福利在线观看| 国产精品久久不能| 国内精品免费午夜毛片| 欧美性猛交xxxx富婆| 亚洲国产精彩中文乱码av| 国产99久久精品一区二区| 亚洲已满18点击进入在线看片| 97精品国产91久久久久久| 国产一区二区三区丝袜| 亚洲欧美日韩一区二区三区在线| 欧美网站在线观看| 最近2019中文字幕第三页视频| 日韩成人高清在线| 这里只有精品丝袜| 亚洲福利在线观看| 亚洲韩国日本中文字幕| 一本色道久久88综合日韩精品| 91视频国产精品| 久久天天躁夜夜躁狠狠躁2022| 日韩欧美一区二区三区久久| 俺去啦;欧美日韩| 91国产一区在线| 欧美成aaa人片免费看| 色噜噜亚洲精品中文字幕| 97精品国产aⅴ7777| 欧美中文字幕在线播放| 日韩av成人在线| 亚洲成年网站在线观看| 69久久夜色精品国产7777| 日韩电影第一页| 亚洲黄色有码视频| 欧美床上激情在线观看| 亚洲一区二区三区在线免费观看| 国产精品69精品一区二区三区| 亚洲视频综合网| 亚洲高清福利视频| 久久成人一区二区| 国产精品一区二区三区久久久| 亚洲国产精久久久久久久| 成人国产精品免费视频| 欧美有码在线观看视频| 欧美大片免费观看在线观看网站推荐| 韩剧1988免费观看全集| 中文字幕久热精品在线视频| 97av在线视频免费播放| 日韩视频欧美视频| 欧美激情中文字幕在线| 日韩在线不卡视频| 草民午夜欧美限制a级福利片| 久久免费视频这里只有精品| 中文字幕精品影院| 欧美有码在线观看视频| 欧美日韩午夜剧场| 欧美在线中文字幕| 国产精品欧美一区二区| 欧美成人在线免费视频| 78色国产精品| 国产精品国产自产拍高清av水多| 亚洲精品一区二区久| 久久久成人精品| 久久精品亚洲精品| 成人在线视频福利| 欧美精品成人91久久久久久久| 久久久天堂国产精品女人| 国产欧美日韩中文字幕| 日韩在线中文视频| 92看片淫黄大片看国产片| 欧美电影免费观看大全| 精品国产一区av| 国产成人亚洲综合| 亚洲第一国产精品| 97久久超碰福利国产精品…| 国产亚洲视频中文字幕视频| 日韩黄色在线免费观看| 亚洲视频在线免费观看| 亚洲人午夜精品免费| 国产精品都在这里| 91精品国产成人www| 91大神在线播放精品| 亚洲国产天堂久久综合| 国产精品欧美一区二区三区奶水|