def time_us(ns, generator, repeats=int(1e6)): """Prints time table for given functions and inputs. functions - dictionary of {func name: func(input)} - functions to time, ns - list of n for which generate input, generator - func(n) - input generation function, repeats - number of times to call functions for each given input.""" generator() print(make_header(['TreeSearch'])) for n in ns: data = read_file("records_1e{0}.txt".format(n)) data_surnames= read_file("records_2e{0}.txt".format(n)) times = [] a=DZ.CreateTree(data) timer = timeit.Timer(lambda: a.TreeSearch(data_surnames)) times.append(timer.timeit(repeats)) print(make_line(10**(n-1), times))
def test_read_file(self): # Using the test file provided, but shouls really test multiline... expectedJsonStr = '{"layout": "home.html", "title": "My awesome site"}' expectedContent = '\nblah blah\n' r = generate.read_file(os.path.join(self.SRC_DIR, 'index.rst')) self.assertEqual((expectedJsonStr, expectedContent), (json.dumps(r[0]), r[1]))
def test_read_file_data(self): data, content = generate.read_file("test/source/contact.rst") self.assertEqual( data, json.loads('{"title": "Contact us!", "layout": "base.html"}')) self.assertEqual(content, "Write an email to [email protected].")
def test_correct_read(self): expected = ({ "title": "My awesome site", "layout": "home.html" }, "\nblah blah\n") result = generate.read_file('test/source/index.rst') self.assertEqual(result, expected)
def test_read_file(self): actual_result = read_file('test/source/contact.rst') expected_result = { 'title': 'Contact us!', 'layout': 'base.html', 'content': '\nWrite an email to [email protected].\n' } self.assertDictEqual(expected_result, actual_result)
def testReadFile(self, mock_open): mock_open.return_value = MagicMock(spec=file) mock_open.return_value.__enter__.return_value = StringIO(INDEX_RST) (metadata, content) = generate.read_file("") self.assertIsInstance(metadata, dict) self.assertIn("layout", metadata) self.assertIsInstance(content, basestring)
def test_reading_correctly(self): expected = ({ "title": "Contact us!", "layout": "base.html" }, "\nWrite an email to [email protected].\n") code_result = generate.read_file('test/source/contact.rst') self.assertEqual(code_result, expected)
def test_reading_no_metadata(self): expected_meta = {} expected_content = "CONTENT" empty_file = open('emptyFile.rst', 'w') empty_file.write("{}\n---\nCONTENT") empty_file.close() metadata, content = generate.read_file('emptyFile.rst') self.assertEqual(metadata, expected_meta) self.assertEqual(content, expected_content)
self.update_heights() # Must update heights before balances self.update_balances() if (self.node != None): print('-' * level * 2, pref, self.node.people, "[" + str(self.height) + ":" + str(self.balance) + "]", 'L' if self.is_leaf() else ' ') if self.node.left != None: self.node.left.display(level + 1, '<') if self.node.right != None: self.node.right.display(level + 1, '>') def CreateTree(inlist): a = AVLTree() for i in inlist: i = i.split(' ') a.insert(i) return a # Usage example if __name__ == "__main__": a = AVLTree() print("----- Inserting -------") inlist = read_file("records_1e1.txt") for i in inlist: i = i.split(' ') a.insert(i) a.display() print(a.search('Aarons'))
def test_read_file(): index_content = ({ "title": "My awesome site", "layout": "home.html" }, "blah blah") assert read_file('test/source/index.rst') == index_content, 'Empty File'
def test_type(): dict_test = dict() assert type( read_file('test/source/index.rst')[0]) == type(dict_test), 'Wrong type'
def test_read_file_correct_input(self): input = 'test/source/contact.rst' actual = generate.read_file(input) expected = {u"title": u"Contact us!", u"layout": u"base.html"}, \ "\nWrite an email to [email protected].\n" self.assertEqual(actual, expected)
def test_read_file_incorrect_file_format(self): actual = generate.read_file(os.path.join(self.path_to_file, self.filename)) expected = {}, '' self.assertEqual(actual, expected)
def test_read_file_inexistent_path(self): actual = generate.read_file(self.inexistent_path) expected = {}, '' self.assertEqual(actual, expected)
for x in array: x = ' '.join(x) check.append(x) return check def bucket_sort(lst): bucket, bucket1, bucket2 = [], [], [] # The three empty buckets # Populating the buckets with the list elements lst = [p.split(' ') for p in lst] for i in range(len(lst)): if lst[i][0][0] in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']: bucket.append(lst[i]) elif lst[i][0][0] in ['J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R']: bucket1.append(lst[i]) elif lst[i][0][0] in ['S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']: bucket2.append(lst[i]) bucket.sort(key=lambda k: k[0]) bucket1.sort(key=lambda k: k[0]) bucket2.sort(key=lambda k: k[0]) final_lst = bucket + bucket1 + bucket2 check = list() for x in final_lst: x = ' '.join(x) check.append(x) return check if __name__ == "__main__": peoples = read_file("records_1e1.txt") write_file("result.txt", insertion_sort(peoples))