diff --git a/docs/source/src.ch02.rst b/docs/source/src.ch02.rst new file mode 100644 index 0000000..7e74bfd --- /dev/null +++ b/docs/source/src.ch02.rst @@ -0,0 +1,22 @@ +src.ch02 package +================ + +Submodules +---------- + +src.ch02.p1\_cleanup\_dictionary module +--------------------------------------- + +.. automodule:: src.ch02.p1_cleanup_dictionary + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: src.ch02 + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/src.rst b/docs/source/src.rst index 2a2c4df..cce5b21 100644 --- a/docs/source/src.rst +++ b/docs/source/src.rst @@ -7,6 +7,7 @@ Subpackages .. toctree:: src.ch01 + src.ch02 Module contents --------------- diff --git a/src/ch02/__init__.py b/src/ch02/__init__.py new file mode 100644 index 0000000..f7cddd4 --- /dev/null +++ b/src/ch02/__init__.py @@ -0,0 +1,14 @@ +"""Chapter 2. + +Attributes: + DICTIONARY_FILE_PATH (str): String with path to Ubuntu 18.04.2's + American English dictionary file. + + CLEANUP_LIST_ERROR (str): String with :py:exc:`IndexError` for Cleanup + Dictionary :func:`~p1_cleanup_dictionary.cleanup_list`. + +""" + +# Constants +DICTIONARY_FILE_PATH = '/usr/share/dict/american-english' +CLEANUP_LIST_ERROR = 'List cannot be empty.' diff --git a/src/ch02/p1_cleanup_dictionary.py b/src/ch02/p1_cleanup_dictionary.py new file mode 100644 index 0000000..b0b8be4 --- /dev/null +++ b/src/ch02/p1_cleanup_dictionary.py @@ -0,0 +1,59 @@ +"""Remove single letter words from a word dictionary.""" +from src.ch01.challenge.c2_name_generator import read_from_file +from src.ch02 import DICTIONARY_FILE_PATH, CLEANUP_LIST_ERROR + + +def cleanup_list(word_list: list) -> list: + """Cleanup word list. + + Remove single letter words from a :py:obj:`list` of words. + + Args: + word_list (list): List with words as elements. + + Returns: + List with words as elements excluding single letter words. + + Raises: + IndexError: If **word_list** is empty. + + """ + if not word_list: + raise IndexError(CLEANUP_LIST_ERROR) + return [word for word in word_list if len(word) > 1] + + +def cleanup_dict(filepath: str) -> list: + """Wrap read_from_file and cleanup_list. + + Passes given **filepath** through + :func:`~src.ch01.challenge.c2_name_generator.read_from_file` + to get a list of words, then :func:`cleanup_list` to remove single letter + words. + + Args: + filepath (str): String with path to word dictionary file. + + Returns: + List with words as elements excluding single letter words. + + """ + return cleanup_list(read_from_file(filepath)) + + +def main(): + """Demonstrate cleanup dictionary.""" + print('I\'m a word dictionary cleaner.\n' + 'I remove those annoying one letter words.\n') + word_list = read_from_file(DICTIONARY_FILE_PATH) + word_list_len = len(word_list) + clean_word_list = cleanup_dict(DICTIONARY_FILE_PATH) + clean_word_list_len = len(clean_word_list) + print(f'Original word list had {word_list_len} words.\n' + f'Cleaned word list has {clean_word_list_len} words.\n' + f'I cleaned up {word_list_len - clean_word_list_len} words! ' + f'Yay, me!') + + +if __name__ == '__main__': + main() diff --git a/tests/data/ch02/dictionary.txt b/tests/data/ch02/dictionary.txt new file mode 100644 index 0000000..4e77037 --- /dev/null +++ b/tests/data/ch02/dictionary.txt @@ -0,0 +1,78 @@ +a +aardvark +abracadabra +b +bee +boson +c +cat +catatonic +d +dog +dirge +e +echo +ebeneezer +f +fox +finicky +g +gecko +gopher +h +hemoglobin +hermit +i +imp +indigo +j +jack-o-lantern +journey +k +kangaroo +kilometer +l +lemon +lime +m +mesolithic +moonlight +n +none +night +o +opaque +opulent +p +penny +pepper +q +quasar +quark +r +riddle +rubber +s +slight +swift +t +tonberry +tomato +u +ultraviolet +umbra +v +venus +vertiginous +w +whip +whirl +x +xena +xenon +y +yacht +yggdrasil +z +zen +zero \ No newline at end of file diff --git a/tests/test_chapter02.py b/tests/test_chapter02.py new file mode 100644 index 0000000..6932eab --- /dev/null +++ b/tests/test_chapter02.py @@ -0,0 +1,37 @@ +"""Test Chapter 2.""" +import os +import unittest +import src.ch02.p1_cleanup_dictionary as cleanup_dictionary +from tests import random_string +from src.ch02 import CLEANUP_LIST_ERROR + + +class TestCleanupDictionary(unittest.TestCase): + """Test Cleanup Dictionary.""" + + def test_bad_type(self): + """Test that it raises an error if word_list is empty.""" + with self.assertRaises(IndexError) as err: + cleanup_dictionary.cleanup_list([]) + self.assertEqual(CLEANUP_LIST_ERROR, err.exception) + + def test_cleanup_list(self): + """Test that it removes single letter words from a list of words.""" + random_list = [random_string(1) for _ in range(13)] + random_list.extend([random_string(5) for _ in range(10)]) + clean_list = cleanup_dictionary.cleanup_list(random_list) + self.assertEqual(len(clean_list), 10) + for element in clean_list: + self.assertEqual(len(element), 5) + + def test_cleanup_dict(self): + """Test that it removes single letter words from a dictionary file.""" + dict_file = os.path.abspath('tests/data/ch02/dictionary.txt') + clean_dict = cleanup_dictionary.cleanup_dict(dict_file) + self.assertEqual(len(clean_dict), 52) # 78 words - 26 letters + for element in clean_dict: + self.assertGreater(len(element), 1) + + +if __name__ == '__main__': + unittest.main()