Skip to content

Instantly share code, notes, and snippets.

@honzakral
Created October 30, 2009 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzakral/222290 to your computer and use it in GitHub Desktop.
Save honzakral/222290 to your computer and use it in GitHub Desktop.
class PageParserAndThumbnailer(object):
def __init__(self, url, http_response):
self.url = url
self.headers, self.body = self.parse(http_response)
@property
def thumbnail(self):
"create expensive thumbnail"
def parse(self, http_response):
"DO NOT TAKE SERIOUSLY"
return http_response.split('\n\n', 1)
def simple_loader(url):
return PageParserAndThumbnailer(urlopen(url))
class CachedLoader(object):
def __init__(self, cache):
self._cache = cache
def __call__(self, url):
try:
out = self._cache.get(url)
except Empty:
out = simple_loader(url)
self._cache.set(url, out)
return out
class WebPage(object):
"""
>>> wp = WebPage("http://example.com")
>>> wp.body
<HTML>
<HEAD>
<TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>You have reached this web page by typing &quot;example.com&quot;,
&quot;example.net&quot;,
or &quot;example.org&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not available
for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
2606</a>, Section 3.</p>
</BODY>
</HTML>
>>> wp.thumbnail
file:///tmp/thumbnails/http_example_com.png
>>> wp = WebPage("http://example.com", loader=CachedLoader(memcache))
"""
def __init__(self, url, loader=simple_loader):
self._loader = loader
self.url = url
def _fetch(self):
self._wrapped = self._loader(self.url)
def __getattr__(self, attname):
if not hasattr(self, '_wrapped'):
self._fetch()
return getattr(self._wrapped, attname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment