Created
March 18, 2022 17:41
-
-
Save nathanjackson/c56dcce6dbba7fe479cca24ce1d2a21e to your computer and use it in GitHub Desktop.
Python Refactoring/Testing Interview Question
This file contains hidden or 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
# This script requests the RSS feed for phoronix.com and outputs the latest | |
# news items into a CSV file. | |
# | |
# It is not particularly well written, and if it were part of a real project, | |
# it would be awkward to test in an automated way. | |
# | |
# How would you refactor (change) this code to make it more testable for fuzzing or unit testing? | |
# | |
import csv | |
import requests | |
import xml.etree.ElementTree as ET | |
url = 'https://www.phoronix.com/rss.php' | |
resp = requests.get(url) | |
with open('topnewsfeed.xml', 'wb') as f: | |
f.write(resp.content) | |
# create element tree object | |
tree = ET.parse("topnewsfeed.xml") | |
# get root element | |
root = tree.getroot() | |
# create empty list for news items | |
newsitems = [] | |
# iterate news items | |
for item in root.findall('./channel/item'): | |
# empty news dictionary | |
news = {} | |
# iterate child elements of item | |
for child in item: | |
# special checking for namespace object content:media | |
if child.tag == '{http://search.yahoo.com/mrss/}content': | |
news['media'] = child.attrib['url'] | |
else: | |
news[child.tag] = child.text.encode('utf8') | |
# append news dictionary to news items list | |
newsitems.append(news) | |
# specifying the fields for csv file | |
fields = ['guid', 'title', 'pubDate', 'description', 'link', 'media'] | |
# writing to csv file | |
with open('output.csv', 'w') as csvfile: | |
# creating a csv dict writer object | |
writer = csv.DictWriter(csvfile, fieldnames = fields) | |
# writing headers (field names) | |
writer.writeheader() | |
# writing data rows | |
writer.writerows(newsitems) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment