Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. # !/usr/local/bin/python3
  2.  
  3. import csv
  4. import argparse
  5.  
  6.  
  7. def load_zip_data(filename='zips.csv'):
  8. """
  9. Loads zipcode data from a CSV file.
  10. :param filename: str - the name of the zipcode file. (zips.csv by default.)
  11. :return dict: Zipcode data, keyed on the zipcode.
  12. """
  13. zips = {}
  14. mras = []
  15. with open(filename, 'r') as zipfile:
  16. zipreader = csv.reader(zipfile, delimiter=",")
  17. next(zipreader, None) # Skip headers
  18. for row in zipreader:
  19. if row[0] in mras:
  20. continue
  21. if row[0] in zips:
  22. dupe = zips.pop(row[0])
  23. if dupe['rate_area'] != row[4]:
  24. mras.append(row[0])
  25. continue
  26. zips[row[0]] = {
  27. 'state': row[1],
  28. 'rate_area': row[4]
  29. }
  30. return zips
  31.  
  32.  
  33. def load_plan_data(filename='plans.csv'):
  34. """
  35. Loads plan data from a CSV file.
  36. :param filename: str - the name of the file with the plan data. (plans.csv by default.)
  37. :return dict: Plan data, keyed on State.
  38. """
  39. plans = {}
  40. with open(filename, 'r') as planfile:
  41. planreader = csv.reader(planfile, delimiter=",")
  42. next(planreader, None) # Skip headers
  43. for row in planreader:
  44. state = row[1]
  45. metal_level = row[2]
  46. rate_area = row[4]
  47. rate = row[3]
  48.  
  49. # We only care about Silver Plans
  50. if metal_level != 'Silver':
  51. continue
  52.  
  53. if state in plans:
  54. state = plans[state]
  55. if rate_area in state:
  56. state[rate_area].append(float(rate))
  57. else:
  58. state[rate_area] = [float(rate)]
  59. else:
  60. plans[state] = {
  61. rate_area: [float(rate)]
  62. }
  63.  
  64. return plans
  65.  
  66.  
  67. def process_data(*args, **kwargs):
  68. """
  69. Processes CSV file containing requested zipcodes, and outputs the data to a CSV file.
  70. """
  71. reasons = kwargs.pop('reasons', False)
  72. if 'zipfile' in kwargs:
  73. zips = load_zip_data(kwargs['zipfile'])
  74. else:
  75. zips = load_zip_data()
  76.  
  77. if 'planfile' in kwargs:
  78. plans = load_plan_data(kwargs['planfile'])
  79. else:
  80. plans = load_plan_data()
  81.  
  82. # Requested Zips
  83. if 'requestfile' in kwargs:
  84. requestfile = kwargs['requestfile']
  85. else:
  86. requestfile = 'slcsp.csv'
  87.  
  88. output = [('zipcode', 'rate')]
  89. with open(requestfile, 'r') as requests:
  90. requestreader = csv.reader(requests, delimiter=',')
  91. next(requestreader) # Skip headers
  92. for row in requestreader:
  93. try:
  94. zipcode = zips[row[0]]
  95. except KeyError:
  96. if reasons:
  97. output.append((row[0], 'Zipcode was in multiple rate areas'))
  98. else:
  99. output.append((row[0], ''))
  100. continue
  101. state = zipcode['state']
  102. rate_area = zipcode['rate_area']
  103. try:
  104. rates = sorted(plans[state][rate_area])
  105. except KeyError:
  106. if reasons:
  107. output.append((row[0], f'{state} state not found'))
  108. else:
  109. output.append((row[0], ''))
  110. continue
  111. if len(rates) > 1:
  112. output.append((row[0], rates[1]))
  113. else:
  114. if reasons:
  115. output.append((row[0], f'Not enough plans!'))
  116. else:
  117. output.append((row[0], ''))
  118.  
  119. with open('output.csv', 'w', newline='') as outfile:
  120. outwriter = csv.writer(outfile, delimiter=',')
  121. outwriter.writerows(output)
  122.  
  123.  
  124.  
  125. if __name__ == "__main__":
  126. parser = argparse.ArgumentParser()
  127. parser.add_argument("--zipfile", help="Specify the name of the file containing all the zipcode data. (Must be CSV Format!)")
  128. parser.add_argument("--planfile", help="Specify the name of the file containing all the plan data. (Must be CSV Format!)")
  129. parser.add_argument("--requestfile", help="Specify the name of the file containing the zipcode requests. (Must be CSV Format!)")
  130. parser.add_argument("--reasons", type=bool, help="If set to true, output will include reasons for missing data.")
  131.  
  132. args = parser.parse_args()
  133.  
  134. kwargs = {}
  135. if args.zipfile:
  136. kwargs['zipfile'] = args.zipfile
  137.  
  138. if args.planfile:
  139. kwargs['planfile'] = args.planfile
  140.  
  141. if args.requestfile:
  142. kwargs['requestfile'] = args.requestfile
  143.  
  144. if args.reasons:
  145. kwargs['reasons'] = args.reasons
  146.  
  147. process_data(**kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement