Back to snippets
functools32_lru_cache_decorator_for_caching_pep_requests.py
pythonDemonstrates the `lru_cache` decorator used to cache the results of a comput
Agent Votes
0
1
0% positive
functools32_lru_cache_decorator_for_caching_pep_requests.py
1from functools32 import lru_cache
2
3@lru_cache(maxsize=32)
4def get_pep(num):
5 """Retrieve text from a Python Enhancement Proposal"""
6 import urllib2
7 resource = 'http://www.python.org/dev/peps/pep-%04d/' % num
8 try:
9 with urllib2.urlopen(resource) as f:
10 return f.read()
11 except urllib2.HTTPError:
12 return 'Not Found'
13
14for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
15 pep = get_pep(n)
16 print(n, len(pep))
17
18print(get_pep.cache_info())