Last active
July 15, 2016 15:41
-
-
Save kevinjcliao/6481e20dfe1ce2e2806db6daa15f8e82 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
#!/usr/bin/env python | |
"""parse_building_codes.py: Parses building code violations provided by CfA""" | |
import csv | |
import datetime | |
__author__ = "Kevin Liao" | |
__copyright__ = "Copyright 2016, Kevin Liao" | |
__license__ = "GPL" | |
class Violation: | |
def __init__( | |
self, | |
violation_category, | |
violation_date, | |
): | |
date = violation_date.split(" ")[0].split("-") | |
year = int(date[0]) | |
month = int(date[1]) | |
day = int(date[2]) | |
self.category = violation_category | |
self.date = datetime.date(year, month, day) | |
with open('./Violations-2012.csv', 'rb') as csvfile: | |
next(csvfile) | |
reader = csv.reader(csvfile) | |
categories = {} | |
for row in reader: | |
violation = Violation(row[2], row[3]) | |
cat = violation.category | |
date = violation.date | |
if(cat not in categories): | |
categories[cat] = {} | |
categories[cat]["number"] = 1 | |
categories[cat]["earliest"] = date | |
categories[cat]["latest"] = date | |
else: | |
categories[cat]["number"] += 1 | |
if date < categories[cat]["earliest"]: | |
categories[cat]["earliest"] = date | |
elif date > categories[cat]["latest"]: | |
categories[cat]["latest"] = date | |
for category, data in categories.iteritems(): | |
print((category + ":").ljust(30) + | |
"Number: " + | |
str(data["number"]).ljust(4) + | |
" Earliest: " + | |
str(data["earliest"]) + | |
" Latest: " + | |
str(data["latest"]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment