1. Jason R. Coombs
  2. jaraco.performance

Commits

Jason R. Coombs  committed 4d11dce

Moved tests to their own modules.
Updated docstrings

  • Participants
  • Parent commits 0e6aadb
  • Branches default

Comments (0)

Files changed (3)

File jaraco/performance/cache.py

View file
  • Ignore whitespace
-# modified from http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/
+"""
+Cache implementations based on Raymond Hettinger's implementations,
+originating with
+http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/
+and now part of Python 3.2 stdlib.
+
+This implementation uses a classed-based approach, which provides a cleaner
+interface, but may not perform as well as the function-based decorators.
+"""
+
 from __future__ import absolute_import
 
 import collections
 	Cache object can be accessed as f.cache, where f is the decorated
 	 function.
 	http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
-
 	'''
 
 	def __init__(self, maxsize=100):
 		self.hits = self.misses = 0
 
 	def decorate(self, user_function,
-		len=len, iter=iter, tuple=tuple, sorted=sorted,
-		KeyError=KeyError
-		):
+			len=len, iter=iter, tuple=tuple, sorted=sorted,
+			KeyError=KeyError
+			):
 
 		sentinel = self.sentinel
 		kwd_mark = self.kwd_mark
 	__call__ = decorate
 
 class LFUCache(Cache):
-	'''Least-frequenty-used cache decorator.
+	'''
+	Least-frequenty-used cache decorator.
 
 	Arguments to the cached function must be hashable.
 	Cache performance statistics stored in .hits and .misses.
 	Cache object can be accessed as f.cache, where f is the decorated
 	 function.
 	http://en.wikipedia.org/wiki/Least_Frequently_Used
-
 	'''
 
 	def __init__(self, maxsize=100):
 		self.store.clear()
 		self.use_count.clear()
 		self.hits = self.misses = 0
-
-
-if __name__ == '__main__':
-
-	@LRUCache(maxsize=20)
-	def f(x, y):
-		return 3*x+y
-
-	domain = range(5)
-	from random import choice
-	for i in range(1000):
-		r = f(choice(domain), choice(domain))
-
-	print(f.cache.hits, f.cache.misses)
-
-	@LFUCache(maxsize=20)
-	def f(x, y):
-		return 3*x+y
-
-	domain = range(5)
-	from random import choice
-	for i in range(1000):
-		r = f(choice(domain), choice(domain))
-
-	print(f.cache.hits, f.cache.misses)

File jaraco/performance/tests/__init__.py

  • Ignore whitespace
Empty file added.

File jaraco/performance/tests/test_cache.py

View file
  • Ignore whitespace
+from __future__ import print_function
+
+from jaraco.performance import cache
+
+def test_LRUCache():
+
+	@cache.LRUCache(maxsize=20)
+	def f(x, y):
+		return 3*x+y
+
+	domain = range(5)
+	from random import choice
+	for i in range(1000):
+		r = f(choice(domain), choice(domain))
+
+	print(f.cache.hits, f.cache.misses)
+
+def test_LFUCache():
+	@cache.LFUCache(maxsize=20)
+	def f(x, y):
+		return 3*x+y
+
+	domain = range(5)
+	from random import choice
+	for i in range(1000):
+		r = f(choice(domain), choice(domain))
+
+	print(f.cache.hits, f.cache.misses)