Created
October 30, 2009 12:05
-
-
Save honzakral/222290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "example.com", | |
"example.net", | |
or "example.org" 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