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_lowercase as keys.

Raises

TypeError – If dictionary is not a dict.

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() then add_keys_to_dict() to fill in missing keys.

Parameters

sentence (str) – String to count letters of.

Returns

Dictionary with string.ascii_lowercase as keys and a list with letters repeated based on their frequency as values.

src.ch01.challenge.c1_foreign_bar_chart.main()[source]

Demonstrates the Foreign Bar Chart.

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.

Parameters
  • name (str) – Name to add to dictionary.

  • key (str) – Key to add name under.

  • dictionary (dict) – Dictionary to add name to.

Returns

None. name is added under key if not present, dictionary is unchanged otherwise.

Raises

TypeError – If name and key aren’t str or if dictionary isn’t a dict.

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 .txt files.

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.main()[source]

Demonstrate name generator.

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 a list, then split_names() to split them into a dict, and finally through generate_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, and last as keys and names as values.

Raises

Note

Drops suffix and adds nickname to middle names.

Module contents

Chapter 1 Challenge Projects.

src.ch01.challenge.ADD_KEYS_ERROR

String with TypeError for add_keys_to_dict().

Type

str

src.ch01.challenge.SPLIT_NAME_LIST_ERROR

String with TypeError for split_names().

Type

str

src.ch01.challenge.SPLIT_NAME_EMPTY_ERROR

Sting with ValueError for split_names().

Type

str

src.ch01.challenge.ADD_NAME_TO_KEY_ERROR

String with TypeError for add_name_to_key().

Type

str

src.ch01.challenge.GENERATE_NAME_ERROR

String with KeyError for generate_name().

Type

str

src.ch01.challenge.BUILD_LIST_ERROR

String with IndexError for build_name_list().

Type

str