def example_chaining_stuff(): """ Example use of the chaingin hash table. This example uses a chaining hash table to store the flags for some number plates, ie, a small database of flags for plates. """ plate1 = NumberPlate('BAA754') plate2 = NumberPlate('MOO123') plate3 = NumberPlate('WOF833') plate4 = NumberPlate('EEK001') table = ChainingHashTable(5) table[plate1] = 'Sheep' table[plate2] = 'Cow' table[plate3] = 'Dog' table[plate4] = 'Mouse' table[plate4] = 'Mouse-updated' print(table) print('plate1 in table:', plate1 in table) print('plate2 in table:', plate2 in table) print('plate3 in table:', plate3 in table) print('plate4 in table:', plate4 in table) print() print('Value for plate1:', table[plate1]) print('Value for plate2:', table[plate2]) print('Value for plate3:', table[plate3]) print('Value for plate4:', table[plate4]) print()
def example_list_table_stuff(): table = ListTable() plate1 = NumberPlate('ABC123') table[plate1] = 'Banana 3' plate2 = NumberPlate('BOB456') table[plate2] = 'Banana 2' plate3 = NumberPlate('JOE234') table[plate3] = 'Orange 1' table[plate3] = 'Orange 1-update' print(plate1 in table) # translates to table.__contains__(flag) print(table[plate3]) # translates to table.__get_item__(plate) print(table)
def example_linearhash_stuff(): """ This example uses a linear hash table to store the flags for some number plates, ie, a small database of flags for plates. """ plate = NumberPlate('AAA000') flag = 'Overdue fines' plate2 = NumberPlate('AAA001') flag2 = '' plate8 = NumberPlate('AAA001') flag8 = '' plate3 = NumberPlate('AAA002') flag3 = 'jonathan' plate4 = NumberPlate('AAA003') flag4 = 'jo' plate5 = NumberPlate('AAA004') flag5 = 'jo' plate6 = NumberPlate('AAA004') flag6 = 'jo' plate7 = NumberPlate('AAA005') flag7 = 'jo' table = LinearHashTable(6) table[plate] = flag # translates to table.__set_item__(flag) table[plate2] = flag2 table[plate8] = flag8 + '-updated' table[plate3] = flag3 table[plate4] = flag4 table[plate5] = flag5 + '-updated' table[plate6] = flag6 + '-pass' table[plate7] = flag7 + '-completed' print(plate in table) # translates to table.__contains__(flag) print(table[plate]) # translates to table.__get_item__(plate) print(table)
def test_records_sequence(max_records): """ Generates a sequence of (number_plate, flag) records (NumberPlate('AAA000'), 'AAA000_test_1') (NumberPlate('AAA001'), 'AAA001_test_2') (NumberPlate('AAA002'), 'AAA002_test_3') etc... n_slots is only used if with_unique_hash is True """ records = [] count = 0 for char1 in string.ascii_uppercase: for char2 in string.ascii_uppercase: for char3 in string.ascii_uppercase: for digit1 in string.digits: for digit2 in string.digits: for digit3 in string.digits: plate_str = char1 + char2 + char3 plate_str += digit1 + digit2 + digit3 flag = '{}_{}_{}'.format( plate_str, TEST_FLAG, count) number_plate = NumberPlate(plate_str) record = (number_plate, flag) records.append(record) count += 1 #print(count) if count >= max_records: return records return records
def table_test_not_contains(self, n_plates, n_slots, records=None, unique_hash=False): self.setUp() # needed each time as subtests don't run setup table = self.table_class(n_slots) for plate, flag in record_sequence(n_plates, n_slots, unique_hash): table[plate] = flag self.assertFalse(NumberPlate('ZZZ999') in table) return True # only happens if passes
def trial_hash_table(plate_str_list, table): for plate_str in [ 'ABC123', 'DUF721', 'EGG001', 'CSC001', 'ACC101', 'BIG404' ]: x = NumberPlate(plate_str) table[x] = 'Stolen' + str(x) assert table[x] == 'Stolen' + str(x) print(table) print() print(table.condensed_str())
def read_timestamped_block(lines, index): plate_list = [] num_lines = len(lines) done = False while index < num_lines and not done: current_line = lines[index] if current_line != '\n': plate, timestamp = current_line.strip().split(';') record = (NumberPlate(plate), timestamp) plate_list.append(record) #print(record) index += 1 else: done = True return plate_list, index
def basic_db_list(plate_strings): """ Takes a list of strings and makes a list of tuples containing (number_plate, flag) pairs. Each number_plate will be a NumberPlate made from the strings. The flags will all be str(plate)+'_flag' This function will be helpful for simple testing :) >>> example = basic_db_list(['ABC123', 'DEF123', 'BAD321']) >>> print(example) [('ABC123', 'ABC123_flag'), ('DEF123', 'DEF123_flag'), ('BAD321', 'BAD321_flag')] """ result = [] for plate_str in plate_strings: plate = NumberPlate(plate_str) record = (plate, str(plate) + '_flag') result.append(record) return result
def read_db_block(lines, index): """ Makes a list of all the (plate,flag) tuples starting from the line with the given index, up until a blank line is reached - the blank line indicates the end of the section of plate date. Returns the list of number plates along with the index of the blank line """ plate_list = [] num_lines = len(lines) done = False while index < num_lines and not done: current_line = lines[index] if current_line != '\n': plate, flag = current_line.strip().split(':') record = (NumberPlate(plate), flag) plate_list.append(record) #print(record) index += 1 else: done = True return plate_list, index