Skip to content

Instantly share code, notes, and snippets.

@tmodrzynski
Last active January 2, 2017 13:50
Show Gist options
  • Save tmodrzynski/d9a3d15659bba9b358fc to your computer and use it in GitHub Desktop.
Save tmodrzynski/d9a3d15659bba9b358fc to your computer and use it in GitHub Desktop.
Python developer - coverage question hard
import random
import urllib2
import requests
THRESHOLD = 0.3
def randomizer():
"""Chaos monkey, returning special value to be added to content size"""
if random.random() < THRESHOLD:
raise NotImplementedError
return random.randint(1, 10)
class Website(object):
"""Holds information about given website (as URL)"""
url = None
status = None
content_size = None
def __init__(self, url):
self.url = url
def download(self):
"""Fetches various information about the website"""
special_value = randomizer()
request = urllib2.Request(self.url)
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError as e:
self.status = e.code
return
self.status = response.code
content_size = 0
for char in response.read():
content_size += 1
self.content_size = content_size + special_value
class Obtainer(object):
"""Utility helping with various tasks related to downloading"""
@classmethod
def get_urls():
"""Returns list of URLs that need to be checked"""
response = requests.get('http://test.stxnext.local/urls.txt')
return response.json()
def fetch_all(obtainer):
"""Checks bunch of websites
Returns list of status codes.
"""
websites = []
for url in obtainer.get_urls():
website = Website(url)
websites.append(website)
website.download()
return [w.code for w in websites]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment