def add_prices(csvtext, drug_dict): # adds prices from a CSV file (the date is the str csvtext arg) to each drug in a given drug dictionary. # for drugs in the dictionary but not in the file, the function adds None as the price. # this function passes on drugs in the CSV but not in the drug_dict. date = csvtext[-8:] count = 1 headers = which_headers(date) drug_list = [drug for drug in drug_dict] with open(csvtext+".csv", "r") as drugcsv: drugreader = csv.reader(drugcsv) for line in drugreader: if count < 5: pass else: drug = {} for i in range(len(headers)): drug[headers[i]] = line[i] price = drug["Price"] if drug["NDC"] in drug_dict: Drug.add_price(drug_dict[drug["NDC"]], date, price) drug_list.remove(drug["NDC"]) else: pass count += 1 drugcsv.close() for drug in drug_list: Drug.add_price(drug_dict[drug], date, None) return drug_dict
def builder(csvtext): # The function that takes a given CSV filename/date and creates a dictionary of drugs. # Their key is the drug NDC, and their value is the Drug object. date = csvtext[-8:] drug_dict = {} count = 1 headers = which_headers(date) with open(csvtext+".csv", "r") as drugcsv: drugreader = csv.reader(drugcsv) for line in drugreader: if count < 5: pass else: drug = {} for i in range(len(headers)): drug[headers[i]] = line[i] price = drug["Price"] this_drug = Drug(drug["Name"], drug["NDC"], drug["Pricing Unit"], drug["OTC or Not"], drug["Brand or Generic"], "NADAC") Drug.add_price(this_drug, date, price) drug_dict[drug["NDC"]] = this_drug count += 1 drugcsv.close() return drug_dict
def update(csvtext, drug_dict): # accesses a one-date CSV file (date given as str csvtext) and adds prices for drugs in the given drug dictionary. # if a drug is not in the drug dict, this function adds the drug and its info to the dictionary. date = csvtext[-8:] count = 1 headers = which_headers(date) with open(csvtext+".csv", "r") as drugcsv: drugreader = csv.reader(drugcsv) for line in drugreader: if count < 5: pass else: drug = {} for i in range(len(headers)): drug[headers[i]] = line[i] price = drug["Price"] if drug["NDC"] in drug_dict: Drug.add_price(drug_dict[drug["NDC"]], date, price) else: if int(date) < 20130215: drug["Pricing Unit"] = "Unknown" drug["Brand or Generic"] = "Unknown" this_drug = Drug(drug["Name"], drug["NDC"], drug["Pricing Unit"], drug["OTC or Not"], drug["Brand or Generic"], "NADAC") Drug.add_price(this_drug, date, price) drug_dict[drug["NDC"]] = this_drug count += 1 drugcsv.close() return drug_dict