src.ch01.challenge package¶
Submodules¶
src.ch01.challenge.c1_foreign_bar_chart module¶
Return letter ‘bar chart’ of a non-English sentence.
-
src.ch01.challenge.c1_foreign_bar_chart.add_keys_to_dict(dictionary: dict) → dict[source]¶ Add keys to dictionary.
Check keys of a letter dictionary and add missing letters.
- Parameters
dictionary (dict) – Dictionary to check keys of.
- Returns
Dictionary with
string.ascii_lowercaseas keys.- Raises
-
src.ch01.challenge.c1_foreign_bar_chart.foreign_freq_analysis(sentence: str) → dict[source]¶ Wrap freq_analysis and add_keys_to_dict.
Passes given sentence through
freq_analysis()thenadd_keys_to_dict()to fill in missing keys.- Parameters
sentence (str) – String to count letters of.
- Returns
Dictionary with
string.ascii_lowercaseas keys and alistwith letters repeated based on their frequency as values.
src.ch01.challenge.c2_name_generator module¶
Generate pseudo-random names from a list of names.
-
src.ch01.challenge.c2_name_generator.add_name_to_key(name: str, dictionary: dict, key: str) → None[source]¶ Add name to key in dictionary.
Add name to dictionary under key if not already present.
-
src.ch01.challenge.c2_name_generator.build_name_list(folderpath: str) → list[source]¶ Build name list from folder.
Builds list of names from name files in given folder.
- Parameters
folderpath (str) – Path to folder with name files.
- Returns
List with names from folderpath.
- Raises
IndexError – If folderpath has no
.txtfiles.
-
src.ch01.challenge.c2_name_generator.generate_name(name_dict: dict) → str[source]¶ Generate pseudo-random name.
Use names in dictionary to generate a random name.
- Parameters
name_dict – Dictionary from
split_names().- Returns
String with a random name.
- Raises
KeyError – If there aren’t three keys in the dictionary.
Note
Only add middle name between 1/3 and 1/4 of the time.
-
src.ch01.challenge.c2_name_generator.name_generator(folderpath: str) → str[source]¶ Wrap generate_name, split_names, and build_name_list.
Passes given folderpath through
build_name_list()to get the names in alist, thensplit_names()to split them into adict, and finally throughgenerate_name()to make the actual name.- Parameters
folderpath (str) – Path to folder with name files.
- Returns
String with pseudo-random name.
-
src.ch01.challenge.c2_name_generator.read_from_file(filepath: str) → list[source]¶ Read from file.
Reads lines from text file and returns a
list.- Parameters
filepath (str) – Path to file with names.
- Returns
List with each line from the file as an element.
Note
Removes trailing whitespaces.
-
src.ch01.challenge.c2_name_generator.split_names(name_list: list) → dict[source]¶ Split names from list of names.
Splits first, middle, and last names from a given list of names.
- Parameters
name_list (list) – List with names as elements.
- Returns
Dictionary of lists with
first,middle, andlastas keys and names as values.- Raises
ValueError – If given name list is empty.
Note
Drops suffix and adds nickname to middle names.
Module contents¶
Chapter 1 Challenge Projects.
-
src.ch01.challenge.ADD_KEYS_ERROR¶ String with
TypeErrorforadd_keys_to_dict().- Type
-
src.ch01.challenge.SPLIT_NAME_LIST_ERROR¶ String with
TypeErrorforsplit_names().- Type
-
src.ch01.challenge.SPLIT_NAME_EMPTY_ERROR¶ Sting with
ValueErrorforsplit_names().- Type
-
src.ch01.challenge.ADD_NAME_TO_KEY_ERROR¶ String with
TypeErrorforadd_name_to_key().- Type
-
src.ch01.challenge.GENERATE_NAME_ERROR¶ String with
KeyErrorforgenerate_name().- Type
-
src.ch01.challenge.BUILD_LIST_ERROR¶ String with
IndexErrorforbuild_name_list().- Type