def download(url, **headers): """Downloads content of given URL. Additional headers can be passed as keyword arguments. """ def header_arg_to_name(header): """Convert header argument (e.g. user_agent) into actual HTTP header name (e.g. User-Agent). """ parts = header.split("_") return "-".join(part.capitalize() for part in parts) headers = dicts.mapkeys(header_arg_to_name, headers) headers.setdefault("User-Agent", "seejoo") try: req = urllib2.Request(url, headers=headers) return urllib2.urlopen(req).read() except ValueError: return download("http://" + url, **headers) except IOError: return None
def test_map(self): self.assertEquals(self.MAPPED_DICT, __unit__.mapkeys(MapKeys.FUNCTION, self.DICT))
def test_dict__empty(self): self.assertEquals({}, __unit__.mapkeys(None, {})) self.assertEquals({}, __unit__.mapkeys(MapKeys.FUNCTION, {}))
def test_dict__some_object(self): with self.assertRaises(TypeError): __unit__.mapkeys(MapKeys.FUNCTION, object())
def test_dict__none(self): with self.assertRaises(TypeError): __unit__.mapkeys(MapKeys.FUNCTION, None)
def test_function__non_function(self): with self.assertRaises(TypeError): __unit__.mapkeys(object(), self.DICT)
def test_function__none(self): self.assertEquals(self.DICT, __unit__.mapkeys(None, self.DICT))