Last active
August 29, 2015 14:25
-
-
Save lostmarinero/77380d29f6beb2a00af7 to your computer and use it in GitHub Desktop.
This is the challenge as submitted to apply to the Code For America Fellowship
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
import csv | |
def convert_to_lowercase(input_word): | |
return input_word[0].lower() + input_word[1:] | |
def convert_to_python_style(input_word): | |
return '_'.join( | |
[ | |
convert_to_lowercase(individual_word) | |
for individual_word | |
in input_word.split(' ') | |
] | |
) | |
def convert_to_human_readable(input_word): | |
return " ".join( | |
[individual_word.capitalize() | |
for individual_word | |
in input_word.split('_') | |
] | |
) | |
class CensusTract(object): | |
def __init__(self, **kwargs): | |
for key, value in kwargs.iteritems(): | |
key = convert_to_python_style(key) | |
if isinstance(value, basestring) and value.isdigit(): | |
value = int(value) | |
self.__setattr__(key, value) | |
@property | |
def population_density(self): | |
return float(self.population) / float(self.land_area) | |
@property | |
def housing_density(self): | |
return float(self.housing_units) / float(self.land_area) | |
def population_density_string(self): | |
return "The population density of Census Tract {0} is {1} per square ?".format(self.name, self.population_density) | |
def housing_density_string(self): | |
return "The population density of Census Tract {0} is {1} per square ?".format(self.name, self.housing_density) | |
class AllTracts(object): | |
def __init__(self): | |
self.tracts = [] | |
def add_tract(self, tract): | |
self.tracts.append(tract) | |
def order_tracts_by_key(self, lookup_key, reverse=False): | |
if len(self.tracts) == 0: | |
raise Exception('There are no tracts currently saved') | |
elif not hasattr(self.tracts[0], lookup_key): | |
raise Exception('The tract object does not have the key {0}'.format(lookup_key)) | |
return sorted(self.tracts, key=lambda x: getattr(x, lookup_key), reverse=reverse) | |
def get_lowest_tract(self, lookup_key): | |
return self.order_tracts_by_key(lookup_key)[0] | |
def lowest_tract_string(self, lookup_key): | |
tract = self.get_lowest_tract(lookup_key) | |
attribute = convert_to_human_readable(lookup_key) | |
return "The Lowest {0} Tract is {1} with a {0} of {2}".format(attribute, tract.name, getattr(tract, lookup_key)) | |
def get_highest_tract(self, lookup_key): | |
return self.order_tracts_by_key(lookup_key)[-1] | |
def highest_tract_string(self, lookup_key): | |
tract = self.get_highest_tract(lookup_key) | |
attribute = convert_to_human_readable(lookup_key) | |
return "The Highest {0} Tract is {1} with a {0} of {2}".format(attribute, tract.name, getattr(tract, lookup_key)) | |
def print_tracts_by_key(self, lookup_key, reverse=False): | |
for tract in self.order_tracts_by_key(lookup_key, reverse=reverse): | |
if lookup_key == 'housing_density': | |
print tract.housing_density_string() | |
elif lookup_key == 'population_density': | |
print tract.population_density_string() | |
else: | |
print getattr(tract, lookup_key) | |
with open('tracts.txt') as import_file: | |
all_tracts = AllTracts() | |
reader = csv.DictReader(import_file, delimiter='\t') | |
for row in reader: | |
all_tracts.add_tract(CensusTract(**row)) | |
print "##############################" | |
print "ALL TRACTS BY POPULATION DENSITY" | |
all_tracts.print_tracts_by_key('population_density') | |
print "##############################" | |
print "##############################" | |
print "ALL TRACTS BY HOUSING DENSITY" | |
all_tracts.print_tracts_by_key('housing_density') | |
print "##############################" | |
print all_tracts.highest_tract_string('population_density') | |
print all_tracts.lowest_tract_string('population_density') | |
print "##############################" | |
print all_tracts.highest_tract_string('housing_density') | |
print all_tracts.lowest_tract_string('housing_density') | |
print "##############################" | |
# print "#convert_to_lowercase converts the first letter to lowercase" | |
# print convert_to_lowercase('Hello') == 'hello' | |
# print "#convert_to_lowercase converts only the first letter to lowercase" | |
# print convert_to_lowercase('HELLO') == 'hELLO' | |
# print "#convert_to_lowercase will not raise an error if it is already lowercase" | |
# print convert_to_lowercase('hello') == 'hello' | |
# print "#convert_to_python_style will work with a single word" | |
# print convert_to_python_style('Hello') == 'hello' | |
# print "#convert_to_python_style will downcase and link a 2 words divided by a space" | |
# print convert_to_python_style('Hello World') == 'hello_world' | |
# print "#convert_to_python_style will convert two separate styles" | |
# print convert_to_python_style('Hello world') == 'hello_world' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment