def test_write_lines(self): lines = 'Hello world' path = 'test.txt' FileHandler.write_lines(path, lines) with open(path, mode='r') as test_file: data = test_file.read() self.assertIsNotNone(data)
def query_definition(self, word): word = word.lower() found = self.dict.get(word) if found is None: close_matches = difflib.get_close_matches(word, self.dict.keys()) return f'Match not Found.\nInstead try: {close_matches}' else: found_str = Dictionary.get_as_str(found) FileHandler.write_lines('Queries.txt', word + ': ' + found_str + '\n') return found_str
def query_definition(self, key): """ Searches for key in dictionary and returns it's definition(s) if found :param key: search term :return: definition of search term or "key not found" if not found """ search_key = key.lower() for dict_key in self._dictionary.keys(): if str(dict_key).lower() == search_key: print("Key found") FileHandler.write_lines(self._out_path, [dict_key, self._dictionary[dict_key]]) return self._dictionary[dict_key] print("Key not found") return "Key not found"
def load_dictionary(self, filepath): try: self.dict = FileHandler.load_data(filepath, FileExtensions.Json) except FileNotFoundError: print('Invalid file path') except: print('Unknown exception occurred')
def load_dictionary(self, filepath): """ Loads dictionary from file at filepath :param filepath: :return: None """ extension = Path(filepath).suffix self._dictionary = FileHandler.load_data(filepath, extension) self._is_loaded = True
def test_load_data(self): data = FileHandler.load_data('data.json', FileExtensions.Json) self.assertIsNotNone(data)
def test_init(self): path = "/Users/Iangs/Desktop/UnitTestOutput.txt" text = ["test", "success"] FileHandler.write_lines(path, text) file_exists = Path(path).is_file() self.assertEqual(file_exists, True)