Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1.  
  2. import csv
  3.  
  4. def convert_to_lowercase(input_word):
  5. return input_word[0].lower() + input_word[1:]
  6.  
  7. def convert_to_python_style(input_word):
  8. return '_'.join(
  9. [
  10. convert_to_lowercase(individual_word)
  11. for individual_word
  12. in input_word.split(' ')
  13. ]
  14. )
  15.  
  16. def convert_to_human_readable(input_word):
  17. return " ".join(
  18. [individual_word.capitalize()
  19. for individual_word
  20. in input_word.split('_')
  21. ]
  22. )
  23.  
  24.  
  25. class CensusTract(object):
  26. def __init__(self, **kwargs):
  27. for key, value in kwargs.iteritems():
  28. key = convert_to_python_style(key)
  29. if isinstance(value, basestring) and value.isdigit():
  30. value = int(value)
  31. self.__setattr__(key, value)
  32.  
  33. @property
  34. def population_density(self):
  35. return float(self.population) / float(self.land_area)
  36.  
  37. @property
  38. def housing_density(self):
  39. return float(self.housing_units) / float(self.land_area)
  40.  
  41. def population_density_string(self):
  42. return "The population density of Census Tract {0} is {1} per square ?".format(self.name, self.population_density)
  43.  
  44. def housing_density_string(self):
  45. return "The population density of Census Tract {0} is {1} per square ?".format(self.name, self.housing_density)
  46.  
  47.  
  48. class AllTracts(object):
  49. def __init__(self):
  50. self.tracts = []
  51.  
  52. def add_tract(self, tract):
  53. self.tracts.append(tract)
  54.  
  55. def order_tracts_by_key(self, lookup_key, reverse=False):
  56. if len(self.tracts) == 0:
  57. raise Exception('There are no tracts currently saved')
  58. elif not hasattr(self.tracts[0], lookup_key):
  59. raise Exception('The tract object does not have the key {0}'.format(lookup_key))
  60. return sorted(self.tracts, key=lambda x: getattr(x, lookup_key), reverse=reverse)
  61.  
  62. def get_lowest_tract(self, lookup_key):
  63. return self.order_tracts_by_key(lookup_key)[0]
  64.  
  65. def lowest_tract_string(self, lookup_key):
  66. tract = self.get_lowest_tract(lookup_key)
  67. attribute = convert_to_human_readable(lookup_key)
  68. return "The Lowest {0} Tract is {1} with a {0} of {2}".format(attribute, tract.name, getattr(tract, lookup_key))
  69.  
  70. def get_highest_tract(self, lookup_key):
  71. return self.order_tracts_by_key(lookup_key)[-1]
  72.  
  73. def highest_tract_string(self, lookup_key):
  74. tract = self.get_highest_tract(lookup_key)
  75. attribute = convert_to_human_readable(lookup_key)
  76. return "The Highest {0} Tract is {1} with a {0} of {2}".format(attribute, tract.name, getattr(tract, lookup_key))
  77.  
  78. def print_tracts_by_key(self, lookup_key, reverse=False):
  79. for tract in self.order_tracts_by_key(lookup_key, reverse=reverse):
  80. if lookup_key == 'housing_density':
  81. print tract.housing_density_string()
  82. elif lookup_key == 'population_density':
  83. print tract.population_density_string()
  84. else:
  85. print getattr(tract, lookup_key)
  86.  
  87.  
  88.  
  89. with open('tracts.txt') as import_file:
  90. all_tracts = AllTracts()
  91. reader = csv.DictReader(import_file, delimiter='\t')
  92. for row in reader:
  93. all_tracts.add_tract(CensusTract(**row))
  94.  
  95.  
  96. print "##############################"
  97. print "ALL TRACTS BY POPULATION DENSITY"
  98. all_tracts.print_tracts_by_key('population_density')
  99. print "##############################"
  100. print "##############################"
  101. print "ALL TRACTS BY HOUSING DENSITY"
  102. all_tracts.print_tracts_by_key('housing_density')
  103. print "##############################"
  104. print all_tracts.highest_tract_string('population_density')
  105. print all_tracts.lowest_tract_string('population_density')
  106. print "##############################"
  107. print all_tracts.highest_tract_string('housing_density')
  108. print all_tracts.lowest_tract_string('housing_density')
  109. print "##############################"
  110.  
  111.  
  112. # print "#convert_to_lowercase converts the first letter to lowercase"
  113. # print convert_to_lowercase('Hello') == 'hello'
  114.  
  115. # print "#convert_to_lowercase converts only the first letter to lowercase"
  116. # print convert_to_lowercase('HELLO') == 'hELLO'
  117.  
  118. # print "#convert_to_lowercase will not raise an error if it is already lowercase"
  119. # print convert_to_lowercase('hello') == 'hello'
  120.  
  121. # print "#convert_to_python_style will work with a single word"
  122. # print convert_to_python_style('Hello') == 'hello'
  123.  
  124. # print "#convert_to_python_style will downcase and link a 2 words divided by a space"
  125. # print convert_to_python_style('Hello World') == 'hello_world'
  126.  
  127. # print "#convert_to_python_style will convert two separate styles"
  128. # print convert_to_python_style('Hello world') == 'hello_world'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement