def table_from_csv(self, file_name='CSV_Data\packages.csv'): ''' Loads data from csv file into package_table. Creates package based on CSV data and inserts that package into hash table Space-time complexity = O(N) ''' # Opens & reads from csv file with open(file_name) as csv_file: reader = csv.reader(csv_file, delimiter=',') # Loop through each row in file, create package based on it's information for row in reader: # Retrieves & sets package attributes package = Package(int(row[0])) package.address_id = row[1] package.address = row[2] package.city = row[3] package.state = row[4] package.zip_code = row[5] package.deadline = row[6] package.weight = row[7] package.instructions = row[8] # Inserts package self.insert_package(package)
def create_new_package(): ''' Prompts user for new package attributes and creates a package based on them. The method will also dynamically assign package & address ID to the package. If there is room in the hash table, the package is then inserted into it. If there is no room in the hash table, a bucket is created for the package to be inserted. Space-time complexity = O(N) ''' global receiveing package_id = -1 address_id = -1 # Find empty bucket in Hash Table and sets it's index to package id # This is valid because we are using a direct hash table for i in range(len(receiveing.package_table)): if type(receiveing.package_table[i]) != Package: package_id = i break # If no empty bucket was found, then hash table full. # Append an empty bucket to the hash table and make it's index the package id if package_id == -1: receiveing.package_table.append(None) package_id = len(receiveing.package_table) - 1 # Prompts user to enter package details. print("Please enter package details below.\n") address = input("Address: ") city = input("City: ") state = input("State: ") zip_code = input("Zip: ") weight = input("Weight: ") deadline = input("Deadline: ") instructions = input("Instructions: ") # Checks if address already exists in hash table. # If yes, set address_id to matching address_id. # If no, create a new address_id not already usedl if receiveing.lookup_packages('address', address): address_id = receiveing.lookup_packages('address', address)[0].address_id else: address_id = receiveing.num_addresses # Create package with attributes entered by user package = Package(package_id) package.address_id = address_id package.address = address package.city = city package.state = state package.zip_code = zip_code package.weight = weight package.deadline = deadline package.instructions = instructions # Inserts package into hash table receiveing.insert_package(package)