I've done the following exercise with Automatize the Boring with Python Chapter 8:
Create a Mad Libs program that reads in text files and allows the user
add their own text anywhere ADJECTIVE, NOUN, ADVERB or VERB
appears in the text file. For example, a text file might look like this:
The ADJECTIVE panda went to the NOUN, then the VERB. A NAME nearby
has not been affected by these events. The program would find these
instances and prompt the user to replace them.
Enter an adjective:
Enter a name:
Enter a verb:
Enter a name:
The following text file would then
To be created:
The idiot panda headed for the chandelier and then screamed. Near
The van was not affected by these events. The results should be
printed on the screen and saved in a new text file.
I decided to read all the text files that are in the same folder as the python script.
All newly created files then end with _mad.txt
I would like to know if this is a good solution.
Are there bad practices?
Is the code easy to understand?
What can be improved?
Are there better approaches for some parts of the code?
Do not hesitate to comment on everything you can find.
Heres the code:
"" " Mad Libs Scans all .txt files in the working folder. If a file is found, the file is parsed for the keywords -ADJECTIVE -NAME -ADVERB -VERB Then, the user is prompted to add a replacement word for the keyword. A file with the name
_mad.txt is created in the folder. In this file, the keywords are replaced by the user input "" " bone import import system import re def filenames_in_script_folder (): "" " Returns all file names located in the same folder. like this script running python "" " os.chdir (os.path.dirname (sys.argv)) returns os.listdir (os.getcwd ()) def words_from_file (filename): "" " Read the text file and return all the words it contains "" " file = open (file name) content_file = file.read () file.close () return file_content.split () def ask_for_replace_word (keyword): "" " Request a replacement for the mentioned keyword. Check if the keyword is a vowel to follow the correct English grammar "" " vowel_regex = re.compile (& # 39; ^[aeiou]& # 39;) if vowel_regex.search (keyword): return the entry ("Enter a" + keyword.lower () + " n") return the entry ("Enter a" + keyword.lower () + " n") def replace_word (word, keywords): "" " Replaces the word provided if it matches one of the keywords. Non-alphabetic signs are ignored when comparing keywords. Otherwise, return the word provided "" " no_char_regex = re.compile (& # 39;[^a-zA-Z]& # 39;) clean_word = no_char_regex.sub (& # 39; word) for keywords in keywords: if keyword == keyword: new_word = ask_for_replace_word (keyword) return word.replace (keyword, new_word) word of return def write_data_to_file (data, filename): "" " Writes the provided data to a file. If no file exists, a new one is created first "" " file = open (file name, & # 39; w & # 39;) file.write (data) file.close () def mad_libs (): "" " The main function reads the file, replaces the keywords and writes to a new file "" " for filename in filename_en_script_folder (): if filename.lower (). endswith (& # 39 ;. txt & # 39;): new_words =  replace_a_word = False for word in words_from_file (filename): KEYWORDS = (ADJECTIVE, NAME, VERB, ADVERB) replace = replace_word (word, KEYWORDS) if replace! = word: replace_a_word = True new_words.append (replace) if replaced_a_word: new_data = & # 39; .join (new_words) print (new_data) write_data_to_file (new_data, filename[:-4] + "_mad.txt") mad_libs ()
I'm running Pylint on the code and in the keyword definition in the mad_libs function that gives me:
Severity Code Description Project File Line Deletion Status
Message The variable name "KEYWORDS" does not conform to the naming of snake_case.
Should not this be ALLCAPS as I did because these words are supposed to be constants?