From 7d3fd7df7948c8560c574a5a8ad06d7276d875bb Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Wed, 22 May 2019 23:19:07 -0500 Subject: [PATCH 01/10] Initial commit --- src/ch02/__init__.py | 10 +++++++++ src/ch02/p1_cleanup_dictionary.py | 37 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/ch02/__init__.py create mode 100644 src/ch02/p1_cleanup_dictionary.py diff --git a/src/ch02/__init__.py b/src/ch02/__init__.py new file mode 100644 index 0000000..e6f23fe --- /dev/null +++ b/src/ch02/__init__.py @@ -0,0 +1,10 @@ +"""Chapter 2. + +Attributes: + DICTIONARY_FILE_PATH (str): String with path to Ubuntu 18.04.2's + American English dictionary file. + +""" + +# Constants +DICTIONARY_FILE_PATH = '/usr/share/dict/american-english' diff --git a/src/ch02/p1_cleanup_dictionary.py b/src/ch02/p1_cleanup_dictionary.py new file mode 100644 index 0000000..8d47341 --- /dev/null +++ b/src/ch02/p1_cleanup_dictionary.py @@ -0,0 +1,37 @@ +"""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 + + +def cleanup_dict(filepath: str) -> list: + """Cleanup dictionary. + + Remove single letter words from a word dictionary file. + + Args: + filepath (str): String with path to dictionary file. + + Returns: + List with words as elements excluding single letter words. + + """ + word_list = read_from_file(filepath) + return [word for word in word_list if len(word) > 1] + + +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() From b87f9abfdd50255855f77e3de2899be72ae0a7f0 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Thu, 23 May 2019 00:49:02 -0500 Subject: [PATCH 02/10] Refactor cleanup_list from cleanup_dict --- src/ch02/p1_cleanup_dictionary.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/ch02/p1_cleanup_dictionary.py b/src/ch02/p1_cleanup_dictionary.py index 8d47341..22b68ad 100644 --- a/src/ch02/p1_cleanup_dictionary.py +++ b/src/ch02/p1_cleanup_dictionary.py @@ -3,22 +3,39 @@ from src.ch02 import DICTIONARY_FILE_PATH -def cleanup_dict(filepath: str) -> list: - """Cleanup dictionary. +def cleanup_list(word_list: list) -> list: + """Cleanup word list. - Remove single letter words from a word dictionary file. + Remove single letter words from a :py:obj:`list` of words. Args: - filepath (str): String with path to dictionary file. + word_list (list): List with words as elements. Returns: List with words as elements excluding single letter words. """ - word_list = read_from_file(filepath) 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' From 9ca8eafc831e21503a91d90ab8351eed73dd1a35 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Thu, 23 May 2019 00:49:30 -0500 Subject: [PATCH 03/10] Initial commit --- docs/source/src.ch02.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 docs/source/src.ch02.rst 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: From 12c22fcc82f748ab4d589f39d7a3dd0dbbd1ee48 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Thu, 23 May 2019 00:49:51 -0500 Subject: [PATCH 04/10] Add ch02 --- docs/source/src.rst | 1 + 1 file changed, 1 insertion(+) 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 --------------- From d9a2b805bfeecd5553164a9fbe689b907b948c81 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 24 May 2019 03:31:21 -0500 Subject: [PATCH 05/10] Add and implement CLEANUP_LIST_ERROR to cleanup_list --- src/ch02/__init__.py | 4 ++++ src/ch02/p1_cleanup_dictionary.py | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ch02/__init__.py b/src/ch02/__init__.py index e6f23fe..f7cddd4 100644 --- a/src/ch02/__init__.py +++ b/src/ch02/__init__.py @@ -4,7 +4,11 @@ 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 index 22b68ad..b0b8be4 100644 --- a/src/ch02/p1_cleanup_dictionary.py +++ b/src/ch02/p1_cleanup_dictionary.py @@ -1,6 +1,6 @@ """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 +from src.ch02 import DICTIONARY_FILE_PATH, CLEANUP_LIST_ERROR def cleanup_list(word_list: list) -> list: @@ -14,7 +14,12 @@ def cleanup_list(word_list: list) -> list: 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] From bbeb27fe3a306e76818d55077d43e579f90fb645 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 24 May 2019 03:32:02 -0500 Subject: [PATCH 06/10] Initial commit --- tests/test_chapter02.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/test_chapter02.py diff --git a/tests/test_chapter02.py b/tests/test_chapter02.py new file mode 100644 index 0000000..60fde72 --- /dev/null +++ b/tests/test_chapter02.py @@ -0,0 +1,18 @@ +"""Test Chapter 2.""" +import unittest +import src.ch02.p1_cleanup_dictionary as cleanup_dictionary +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) + + +if __name__ == '__main__': + unittest.main() From b69285df02bb600850a84399e852a9c8a291e651 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 24 May 2019 03:44:36 -0500 Subject: [PATCH 07/10] Add test_cleanup_list to TestCleanupDictionary --- tests/test_chapter02.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_chapter02.py b/tests/test_chapter02.py index 60fde72..bda1c68 100644 --- a/tests/test_chapter02.py +++ b/tests/test_chapter02.py @@ -1,6 +1,7 @@ """Test Chapter 2.""" import unittest import src.ch02.p1_cleanup_dictionary as cleanup_dictionary +from tests import random_string from src.ch02 import CLEANUP_LIST_ERROR @@ -13,6 +14,15 @@ def test_bad_type(self): 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) + if __name__ == '__main__': unittest.main() From f872985082458f09f47e1087686540cb7f100486 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 24 May 2019 04:11:59 -0500 Subject: [PATCH 08/10] Initial commit --- tests/data/ch02/dictionary.txt | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/data/ch02/dictionary.txt 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 From 188d34668bc9dde08a3a225b4fefbe824a2bf581 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 24 May 2019 04:12:25 -0500 Subject: [PATCH 09/10] Add text_cleanup_dict to TestCleanupDictionary --- tests/test_chapter02.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_chapter02.py b/tests/test_chapter02.py index bda1c68..6932eab 100644 --- a/tests/test_chapter02.py +++ b/tests/test_chapter02.py @@ -1,4 +1,5 @@ """Test Chapter 2.""" +import os import unittest import src.ch02.p1_cleanup_dictionary as cleanup_dictionary from tests import random_string @@ -23,6 +24,14 @@ def test_cleanup_list(self): 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() From 53b9c4e52a01a8d8c8d2268ab6e17fca301ea5e6 Mon Sep 17 00:00:00 2001 From: JoseALermaIII Date: Fri, 24 May 2019 04:12:25 -0500 Subject: [PATCH 10/10] Add test_cleanup_dict to TestCleanupDictionary --- tests/test_chapter02.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_chapter02.py b/tests/test_chapter02.py index bda1c68..6932eab 100644 --- a/tests/test_chapter02.py +++ b/tests/test_chapter02.py @@ -1,4 +1,5 @@ """Test Chapter 2.""" +import os import unittest import src.ch02.p1_cleanup_dictionary as cleanup_dictionary from tests import random_string @@ -23,6 +24,14 @@ def test_cleanup_list(self): 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()