Last active
October 17, 2019 16:41
-
-
Save hostelix/eb585059339a1cae87eb3f79e421a3fb to your computer and use it in GitHub Desktop.
DSF - slcsp - Israel lugo
This file contains hidden or 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 | |
| from collections import defaultdict | |
| SILVER_PLAN = 'Silver' | |
| def read_data_csv(filename): | |
| with open(filename, mode='r') as csv_file: | |
| reader = csv.DictReader(csv_file) | |
| return [row for row in reader] | |
| def groupby_country_rate_area(list_plans): | |
| data = defaultdict(list) | |
| for item in list_plans: | |
| key = "{}_{}".format(item['state'], item['rate_area']) | |
| data[key].append(item['rate']) | |
| return dict(data) | |
| def get_data_country_by_zipcode(zipcode, data_zips): | |
| res = list(filter(lambda elm: elm['zipcode'] == zipcode, data_zips)) | |
| zipcodes = list(set([item['zipcode'] for item in res])) | |
| rate_areas = list(set([item['rate_area'] for item in res])) | |
| if len(zipcodes) == 1 and len(rate_areas) == 1: | |
| return [res[0]] | |
| return res | |
| def sort_and_remove_duplicate_rate(data_rates): | |
| for key in data_rates.keys(): | |
| data_rates[key].sort() # Sort | |
| data_rates[key] = list(set(data_rates[key])) # Remove duplicate rate | |
| data_rates[key].sort() # Sort | |
| data_slcsp = read_data_csv('slcsp.csv') | |
| data_plans = read_data_csv('plans.csv') | |
| data_zips = read_data_csv('zips.csv') | |
| data_silver_plans = list( | |
| filter(lambda elm: elm['metal_level'] == SILVER_PLAN, data_plans)) | |
| data_country_rate_area = groupby_country_rate_area(data_silver_plans) | |
| sort_and_remove_duplicate_rate(data_country_rate_area) | |
| with open('slcsp.csv', mode='w') as csv_file: | |
| writer = csv.writer(csv_file, delimiter=',') | |
| writer.writerow(['zipcode', 'rate']) | |
| for item in data_slcsp: | |
| data = get_data_country_by_zipcode(item['zipcode'], data_zips) | |
| if len(data) == 1: | |
| key = "{}_{}".format(data[0]['state'], data[0]['rate_area']) | |
| if key in data_country_rate_area: | |
| val = format(float(data_country_rate_area[key][1]), '.2f') if len( | |
| data_country_rate_area[key]) > 1 else '' | |
| writer.writerow([item['zipcode'], val]) | |
| else: | |
| writer.writerow([item['zipcode'], '']) | |
| else: | |
| writer.writerow([item['zipcode'], '']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment