gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
def step9createproducttable(datafilename, normalizeddatabasefilename): Inputs: Name of…
- User
def step9_create_product_table(data_filename, normalized_database_filename): # Inputs: Name of the data and normalized database filename # Output: None
### BEGIN SOLUTION with open(data_filename, 'r') as f: header = f.readline().split('\t') prodcat_col_index = header.index("ProductCategory") prodname_col_index = header.index("ProductName") produp_col_index = header.index("ProductUnitPrice") prodcatid_dict = step8_create_productcategory_to_productcategoryid_dictionary(normalized_database_filename) product_table = set() for line in f.readlines(): prodCatID = [prodcatid_dict[i] for i in line.split('\t')[prodcat_col_index].split(';')] product_table.update(zip( line.split('\t')[prodname_col_index].split(';'), map(float, line.split('\t')[produp_col_index].split(';')), prodCatID )) product_table = [[i + 1, *j] for i, j in enumerate(sorted(product_table))] conn = create_connection(normalized_database_filename) with conn: create_product_table = ''' CREATE TABLE Product( [ProductID] INTEGER NOT NULL PRIMARY KEY, [ProductName] TEXT NOT NULL, [ProductUnitPrice] REAL NOT NULL, [ProductCategoryID] INTEGER NOT NULL, FOREIGN KEY (ProductCategoryID) REFERENCES ProductCategory (ProductCategoryID) )''' create_table(conn, create_product_table, 'Product') conn.cursor().executemany("INSERT INTO Product VALUES (?,?,?,?)", product_table)Can you rewrite the logic for above code but functionality should be same using dict map or lamda?
- Assistant
Yes, here's a refactored version of the code that uses lambda functions and dictionary mapping:
def step9_create_product_table(data_filename, normalized_database_filename): # Inputs: Name of the data and normalized database filename # Output: None # Define dictionary mapping for product category names to IDs prodcatid_dict = step8_create_productcategory_to_productcategoryid_dictionary(normalized_database_filename) # Define lambda functions for splitting lines and mapping category names to IDs split_line = lambda line: line.strip().split('\t') map_category_to_id = lambda cat_names: [prodcatid_dict[cat] for cat in cat_names.split(';')] with open(data_filename, 'r') as f: # Read header to get column indices header = split_line(f.readline()) prodcat_col = header.index("ProductCategory") prodname_col = header.index("ProductName") produp_col = header.index("ProductUnitPrice") # Create set of tuples representing products products = set() for line in f: prodname, produp, prodcat = map(split_line([prodname_col, produp_col, prodcat_col]), map_category_to_id) products.update(zip(prodname, produp, prodcat)) # Sort and number products as list of lists product_table = [[i+1, *j] for i, j in enumerate(sorted(products))] # Create new table for products and insert into database conn = create_connection(normalized_database_filename) with conn: create_product_table = ''' CREATE TABLE Product( [ProductID] INTEGER NOT NULL PRIMARY KEY, [ProductName] TEXT NOT NULL, [ProductUnitPrice] REAL NOT NULL, [ProductCategoryID] INTEGER NOT NULL, FOREIGN KEY (ProductCategoryID) REFERENCES ProductCategory (ProductCategoryID) )''' create_table(conn, create_product_table, 'Product') conn.cursor().executemany("INSERT INTO Product VALUES (?,?,?,?)", product_table)