Skip to content

Instantly share code, notes, and snippets.

Created Jul 9, 2019
What would you like to do?
import csv
import sys
Gets all silver plans out of plans.csv.
Returns a silverPlans dictionary consisting of
a tuple key and array value.
{(state, rate_area): [...], }
def getSilverPlans():
with open("plans.csv", "r") as plans:
plans = csv.DictReader(plans, delimiter = ",")
silverPlans = {}
for plan in plans:
if plan["metal_level"] == "Silver":
if silverPlans.__contains__((plan["state"], plan["rate_area"])):
rates = silverPlans[(plan["state"], plan["rate_area"])]
silverPlans[(plan["state"], plan["rate_area"])] = rates
silverPlans[(plan["state"], plan["rate_area"])] = [plan["rate"]]
return silverPlans
Get the second lowest cost silver plan out of an array of silver plans.
def getSlcsp(rates):
rates = set(rates) # remove duplicates out of the array
rates = sorted(rates) # sort the list in ascending order
slcsp = float(rates[1])
return "{0:.2f}".format(slcsp)
except IndexError:
return ""
Get the zipcodes for all rate areas in silverPlans dictionary.
Return zipcode dictionary with a zipcode key and rate value.
def getZipCodes(silverPlans):
with open("zips.csv", "r") as zips:
zips = csv.DictReader(zips, delimiter=",")
zipCodes = {}
for zip in zips:
state_RateAreaTuple = (zip["state"], zip["rate_area"])
if silverPlans.__contains__(state_RateAreaTuple):
rate = silverPlans[state_RateAreaTuple]
zipCodes[zip["zipcode"]] = silverPlans[state_RateAreaTuple]
return zipCodes
def getRate(zipcode):
with open("slcsp.csv", "r") as slcps:
slcps = csv.DictReader(slcps, delimiter=",")
for slcp in slcps:
if zipcode == slcp["zipcode"]:
return "slcsp not found"
silverPlans = getSilverPlans()
for rateArea, rates in silverPlans.items():
silverPlans[rateArea] = getSlcsp(rates)
zipCodes = getZipCodes(silverPlans)
zipcode = ""
if len(sys.argv) != 2:
raise Exception("Arguments error")
zipcode = sys.argv[1]
except Exception as e:
print("Error! {}".format(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment