SHARE
TWEET

Untitled

a guest Jul 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. To run:
  3.     1. If csv files are downloaded to 'slcsp' directory in the same hierarchy this file exists
  4.     >>> python calculate_slcsp.py
  5.  
  6.     (OR)
  7.     2. To give custom paths to csv files
  8.     >>> python calculate_slcsp.py <SLCSP_CSV_FILE_PATH> <ZIPS_CSV_FILE_PATH> <PLANS_CSV_FILE_PATH>
  9. """
  10.  
  11. from collections import OrderedDict
  12. import csv
  13. import sys
  14.  
  15. DEFAULT_SLCSP_CSV_FILEPATH = 'slcsp/slcsp.csv'
  16. DEFAULT_ZIPS_CSV_FILEPATH = 'slcsp/zips.csv'
  17. DEFAULT_PLANS_CSV_FILEPATH = 'slcsp/plans.csv'
  18.  
  19.  
  20. def calculate_slcsp(slcsp_csv, zips_csv, plans_csv):
  21.     slcsp = read_csv(slcsp_csv)
  22.     zips = read_csv(zips_csv)
  23.     plans = read_csv(plans_csv)
  24.     silver_plans = get_silver_plans(plans)
  25.  
  26.     zipcode_rate_dict = get_zipcode_rate_area(slcsp, zips)
  27.     slcsp_plans = get_plans_for_zipcode(zipcode_rate_dict, silver_plans)
  28.     write_csv(slcsp_plans)
  29.  
  30.     return
  31.  
  32.  
  33. def get_zipcode_rate_area(slcsp, zips):
  34.     zipcode_rate_dict = OrderedDict()
  35.     for slcsp_row in slcsp:
  36.         slcsp_zipcode = slcsp_row['zipcode']
  37.  
  38.         for zip_row in zips:
  39.             zipcode = zip_row['zipcode']
  40.             rate_area = zip_row['rate_area']
  41.             state = zip_row['state']
  42.  
  43.             if slcsp_zipcode != zipcode:
  44.                 continue
  45.  
  46.             if zipcode in zipcode_rate_dict:
  47.                 if zipcode_rate_dict[zipcode]['rate_area'] != rate_area:
  48.                     # Handling case when zipcode has different multiple rates
  49.                     zipcode_rate_dict[zipcode] = None
  50.                 break
  51.             else:
  52.                 zipcode_rate_dict[zipcode] = {'rate_area': rate_area,
  53.                                               'state': state}
  54.  
  55.         if slcsp_zipcode not in zipcode_rate_dict:
  56.             zipcode_rate_dict[slcsp_zipcode] = None
  57.  
  58.     return zipcode_rate_dict
  59.  
  60.  
  61. def get_plans_for_zipcode(zipcode_rate_dict, plans):
  62.     slcsp_plans = OrderedDict()
  63.     for zipcode in zipcode_rate_dict:
  64.  
  65.         if zipcode_rate_dict[zipcode]:
  66.             rate_area = zipcode_rate_dict[zipcode]['rate_area']
  67.             state = zipcode_rate_dict[zipcode]['state']
  68.  
  69.             for plan in plans:
  70.                 if rate_area == plan['rate_area'] and state == plan['state']:
  71.                     if zipcode not in slcsp_plans:
  72.                         slcsp_plans[zipcode] = []
  73.                     slcsp_plans[zipcode].append(plan['rate'])
  74.         else:
  75.             slcsp_plans[zipcode] = []
  76.  
  77.         if zipcode not in slcsp_plans:
  78.             slcsp_plans[zipcode] = []
  79.  
  80.     return slcsp_plans
  81.  
  82.  
  83. def read_csv(file_name):
  84.     content = []
  85.     csv_dict_reader = csv.DictReader(open(file_name, 'r'))
  86.     for row in csv_dict_reader:
  87.         content.append(row)
  88.     return content
  89.  
  90.  
  91. def write_csv(slcsp_plans):
  92.     csv_rows = []
  93.     csv_rows.append('zipcode,rate')
  94.     for zipcode in slcsp_plans:
  95.         rates = list(set(slcsp_plans[zipcode]))
  96.         sorted_rates = sorted(rates)
  97.         if len(sorted_rates) > 1:
  98.             # Selecting 2nd lowest plan and rounding to two digits
  99.             csv_rows.append('{},{}'.format(zipcode, round(float(sorted_rates[1]),2)))
  100.         else:
  101.             # Handling case when zipcode has only zero or one rate plans
  102.             csv_rows.append('{},{}'.format(zipcode, ''))
  103.     print('\n'.join(csv_rows))
  104.  
  105.  
  106. def get_silver_plans(plans):
  107.     silver_plans = []
  108.     for plan in plans:
  109.         if plan['metal_level'] == 'Silver':
  110.             silver_plans.append(plan)
  111.     return silver_plans
  112.  
  113.  
  114. if __name__ == '__main__':
  115.     if len(sys.argv) == 4:
  116.         slcsp_csv = sys.argv[1]
  117.         zips_csv = sys.argv[2]
  118.         plans_csv = sys.argv[3]
  119.     else:
  120.         slcsp_csv = DEFAULT_SLCSP_CSV_FILEPATH
  121.         zips_csv = DEFAULT_ZIPS_CSV_FILEPATH
  122.         plans_csv = DEFAULT_PLANS_CSV_FILEPATH
  123.  
  124.     calculate_slcsp(slcsp_csv, zips_csv, plans_csv)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top