Skip to content

Commit

Permalink
Issue #430. Extracted class RawInmateData to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
nwinklareth committed Jun 1, 2014
1 parent eb94344 commit ff2c0b2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 61 deletions.
64 changes: 64 additions & 0 deletions scraper/raw_inmate_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

import os.path
import csv
from collections import OrderedDict
import shutil


class RawInmateData:

HEADER_METHOD_NAMES = OrderedDict([
('Booking_Id', 'jail_id'),
('Booking_Date', 'booking_date'),
('Inmate_Hash', 'hash_id'),
('Gender', 'gender'),
('Race', 'race'),
('Height', 'height'),
('Weight', 'weight'),
('Age_At_Booking', 'age_at_booking'),
('Housing_Location', 'housing_location'),
('Charges', 'charges'),
('Bail_Amount', 'bail_amount'),
('Court_Date', 'next_court_date'),
('Court_Location', 'court_house_location')
])

def __init__(self, today, raw_inmate_dir, build_dir):
self.__today = today
self.__raw_inmate_dir = raw_inmate_dir
self.__build_dir = build_dir
self.__build_file_writer = None
self.__build_file = None
self.__build_file_name = None

def add(self, inmate_details):
if self.__build_file_writer is None:
self.__open_build_file()
inmate_info = [
getattr(inmate_details, method_name)() for method_name in RawInmateData.HEADER_METHOD_NAMES.itervalues()
]
self.__build_file_writer.writerow(inmate_info)

def __ensure_year_dir(self):
year_dir = os.path.join(self.__raw_inmate_dir, self.__today.strftime('%Y'))
try:
os.makedirs(year_dir)
except OSError:
if not os.path.isdir(year_dir):
raise
return year_dir

def __file_name(self):
return self.__today.strftime('%Y-%m-%d.csv')

def finish(self):
self.__build_file.close()
year_dir = self.__ensure_year_dir()
shutil.move(self.__build_file_name, year_dir)

def __open_build_file(self):
self.__build_file_name = os.path.join(self.__build_dir, self.__file_name())
self.__build_file = open(self.__build_file_name, "w")
self.__build_file_writer = csv.writer(self.__build_file)
header_names = [header_name for header_name in RawInmateData.HEADER_METHOD_NAMES.iterkeys()]
self.__build_file_writer.writerow(header_names)
62 changes: 1 addition & 61 deletions tests/scraper/test_raw_inmate_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,7 @@
from mock import Mock
import os.path
import csv
from collections import OrderedDict
import shutil


class RawInmateData:

HEADER_METHOD_NAMES = OrderedDict([
('Booking_Id', 'jail_id'),
('Booking_Date', 'booking_date'),
('Inmate_Hash', 'hash_id'),
('Gender', 'gender'),
('Race', 'race'),
('Height', 'height'),
('Weight', 'weight'),
('Age_At_Booking', 'age_at_booking'),
('Housing_Location', 'housing_location'),
('Charges', 'charges'),
('Bail_Amount', 'bail_amount'),
('Court_Date', 'next_court_date'),
('Court_Location', 'court_house_location')
])

def __init__(self, today, raw_inmate_dir, build_dir):
self.__today = today
self.__raw_inmate_dir = raw_inmate_dir
self.__build_dir = build_dir
self.__build_file_writer = None
self.__build_file = None
self.__build_file_name = None

def add(self, inmate_details):
if self.__build_file_writer is None:
self.__open_build_file()
inmate_info = [
getattr(inmate_details, method_name)() for method_name in RawInmateData.HEADER_METHOD_NAMES.itervalues()
]
self.__build_file_writer.writerow(inmate_info)

def __ensure_year_dir(self):
year_dir = os.path.join(self.__raw_inmate_dir, self.__today.strftime('%Y'))
try:
os.makedirs(year_dir)
except OSError:
if not os.path.isdir(year_dir):
raise
return year_dir

def __file_name(self):
return self.__today.strftime('%Y-%m-%d.csv')

def finish(self):
self.__build_file.close()
year_dir = self.__ensure_year_dir()
shutil.move(self.__build_file_name, year_dir)

def __open_build_file(self):
self.__build_file_name = os.path.join(self.__build_dir, self.__file_name())
self.__build_file = open(self.__build_file_name, "w")
self.__build_file_writer = csv.writer(self.__build_file)
header_names = [header_name for header_name in RawInmateData.HEADER_METHOD_NAMES.iterkeys()]
self.__build_file_writer.writerow(header_names)
from scraper.raw_inmate_data import RawInmateData


class TestRawInmateData:
Expand Down

0 comments on commit ff2c0b2

Please sign in to comment.