Guided Project: Building Fast Queries on a CSV

Screen Link:

My Code:

class Inventory():
    
    def __init__(self, csv_filename):
        with open('C:/Users/TimKa/OneDrive/Data Eng Study/Guided Projs/laptops.csv') as file:
            rows = list(csv.reader(file)) 
            self.header = rows[0]
            self.rows = rows[1:] 
            for row in self.rows:
                row[-1] = int(row[-1]) 
            
            self.id_to_row = {} 
            for row in self.rows:
                self.id_to_row[row[0]]= row 
            
            self.prices = set() #step 1
            for row in self.rows: # the row has stored the data for each laptop as a list
                self.prices.add(row[-1]) # step 2
            
            self.rows_by_price = sorted(self.rows, key=row_price)
            for row in self.rows_by_price:
                row[-1] = int(row[-1])
        print(self.rows_by_price)

            def check_range_fast(self, min_price, max_price):
        for price in self.rows_by_price:
            if max_price > price[-1] > min_price: 
                return self.rows_by_price[price[-1]]
        return None   

What I expected to happen:

A list of rows which show all the laptops priced between a given range of numbers

What actually happened:

['7746281', 'Dell', 'Inspiron 3567', 'Notebook', '15.6', 'Full HD 1920x1080', 'Intel Core i3 6006U 2GHz', '4GB', '1TB HDD', 'AMD Radeon R5 M430', 'Windows 10', '2.2kg', 449]

I get a row that is outside my range. I want to write a query that finds all laptops whose price is in the given range.

1 Like

I saw two problems in your code:

  1. The check_range_fast function is never called. So, no matter what it does, it is doing nothing. Also, it’s inside the init function and while this is not a syntax problem, you should double-check if it makes sense. Finally, there are some indentation problems, but I think it can be just the way you pasted the code in the markdown, otherwise, it should be throwing an error.

I see it like this:

         def check_range_fast(self, min_price, max_price):
    for price in self.rows_by_price:
        if max_price > price[-1] > min_price: 
                return self.rows_by_price[price[-1]]

But it should be like this:

 def check_range_fast(self, min_price, max_price):
        for price in self.rows_by_price:
            if max_price > price[-1] > min_price: 
                return self.rows_by_price[price[-1]]
  1. You’re performing the same for loop three times. Again, this is not a syntax error but makes your code less efficient, harder to read and to debug.

This:

for row in self.rows:
    row[-1] = int(row[-1]) 
            
self.id_to_row = {} 
for row in self.rows:
    self.id_to_row[row[0]]= row 
            
self.prices = set() 
for row in self.rows: 
    self.prices.add(row[-1]) 

Can be replaced with this:

self.id_to_row = {} 
self.prices = set() 

for row in self.rows:
    row[-1] = int(row[-1]) 
    self.id_to_row[row[0]]= row 
    self.prices.add(row[-1])

One last thing: the init function returns None. The output you get is because you have a print inside the function. If you don’t want the function to return anything, you can just not use the return clause. Unless you explicitly need it to return the value None, for some reason.

I hope this helps you.

1 Like

Thank you so much @otavios.s really appreciate the feedback. I am still learning!!

1 Like