diff --git a/students/piatkowska_anna/lesson_09_files/extending_the_multiclipboard.pyw b/students/piatkowska_anna/lesson_09_files/extending_the_multiclipboard.pyw new file mode 100644 index 000000000..3b6ba9ef0 --- /dev/null +++ b/students/piatkowska_anna/lesson_09_files/extending_the_multiclipboard.pyw @@ -0,0 +1,41 @@ +''' +Extending the Multiclipboard +Extend the multiclipboard program in this chapter +so that it has a delete command line argument +that will delete a keyword from the shelf. +Then add a delete command line argument that will delete all keywords. +''' +#! python3 +# mcb.pyw - Saves and loads pieces of text to the clipboard. +# Usage: py.exe mcb.pyw save - Saves clipboard to keyword. +# py.exe mcb.pyw - Loads keyword to clipboard. +# py.exe mcb.pyw list - Loads all keywords to clipboard. +# py.exe mcb.pyw delete - delete keyword. +# py.exe mcb.pyw delete - delete all keywords. + +import shelve, pyperclip, sys + + +def multiclipboard(): + mcbShelf = shelve.open('mcb') + # Save clipboard content. + if len(sys.argv) == 3 and sys.argv[1].lower() == 'save': + mcbShelf[sys.argv[2]] = pyperclip.paste() + elif len(sys.argv) == 3 and sys.argv[1].lower() == 'delete': + if sys.argv[2] in mcbShelf.keys(): + del mcbShelf[sys.argv[2]] + elif len(sys.argv) == 2: + # List keywords and load content. + if sys.argv[1].lower() == 'list': + pyperclip.copy(str(list(mcbShelf.keys()))) + elif sys.argv[1].lower() == 'delete': + # delete all keywords + for k in mcbShelf.keys(): + del mcbShelf[k] + elif sys.argv[1] in mcbShelf: + pyperclip.copy(mcbShelf[sys.argv[1]]) + mcbShelf.close() + + +if __name__ == "__main__": + multiclipboard() \ No newline at end of file diff --git a/students/piatkowska_anna/lesson_09_files/mad_libs.py b/students/piatkowska_anna/lesson_09_files/mad_libs.py new file mode 100644 index 000000000..77191cf0d --- /dev/null +++ b/students/piatkowska_anna/lesson_09_files/mad_libs.py @@ -0,0 +1,82 @@ +''' +Create a Mad Libs program that reads in text files and lets +the user add their own text anywhere the word ADJECTIVE, +NOUN, ADVERB, or VERB appears in the text file. +For example, a text file may look like this: + + +The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was +unaffected by these events. +The program would find these occurrences and prompt the user to replace them. + + +Enter an adjective: +silly +Enter a noun: +chandelier +Enter a verb: +screamed +Enter a noun: +pickup truck +The following text file would then be created: + + +The silly panda walked to the chandelier and then screamed. A nearby pickup +truck was unaffected by these events. + +The results should be printed to the screen and saved to a new text file. +''' +# ! python3 +# mad_libs.py - Create new txt file based on user input and file +# specified by user in arguments list. +# Usage: py.exe mad_libs.py - specifies input text file, +# output file will be created in the same directory with "out" suffix. + +import os +import sys + + +def mad_lib(): + searched_words = ['ADJECTIVE', 'NOUN', 'ADVERB', 'VERB'] + user_interaction = ['Enter an adjective:', + 'Enter a noun:', + 'Enter an adverb:', + 'Enter a verb:'] + if len(sys.argv) == 2: + if not os.path.exists(sys.argv[1]): + print("Provided file does not exist. Exit.") + return + in_file = open(sys.argv[1]) + lines = in_file.readlines() + in_file.close() + + out_file = open(sys.argv[1] + 'out', 'w') + out_file_content = '' + for line in lines: + current_index = 0 + find_indexes = [0] * len(searched_words) + while current_index < len(line): + for s_word in searched_words: + find_indexes[searched_words.index(s_word)] = line.find( + s_word, current_index) + min = len(line) + searched_index = -1 + for i in range(len(find_indexes)): + if find_indexes[i] != -1 and find_indexes[i] < min: + min = find_indexes[i] + searched_index = i + if min == len(line): + out_file_content += line[current_index:len(line)] + current_index = len(line) + else: + new_word = input(user_interaction[searched_index]) + out_file_content += line[current_index:min] + out_file_content += new_word + current_index = min + len(searched_words[searched_index]) + out_file.write(out_file_content) + out_file.close() + print(out_file_content) + + +if __name__ == "__main__": + mad_lib() diff --git a/students/piatkowska_anna/lesson_09_files/regex_search.py b/students/piatkowska_anna/lesson_09_files/regex_search.py new file mode 100644 index 000000000..85ebc2091 --- /dev/null +++ b/students/piatkowska_anna/lesson_09_files/regex_search.py @@ -0,0 +1,54 @@ +''' +Write a program that opens all .txt files in a folder +and searches for any line that matches a user-supplied +regular expression. The results should be printed to the screen. +''' +# ! python3 +# regex_search.py - searche all .txt files +# for user specified regular expression +# Usage: py.exe regex_search.py +# - specifies regular expression, +# - path to txt. files if not specified then +# current dir are searched + +import sys +import os +import re + + +def print_usage(): + print("Usage: py.exe regex_search.py ") + print(" - specifies regular expression,") + print(" - path to txt. files, if not specified then") + print(" files from current dir are searched") + + +def regex_search(): + path_to_files = '.' + search_rule = '' + if len(sys.argv) == 3: + path_to_files = sys.argv[2] + search_rule = sys.argv[1] + elif len(sys.argv) == 2: + search_rule = sys.argv[1] + else: + print_usage() + return + try: + for file_name in os.listdir(path_to_files): + try: + if file_name.endswith(".txt"): + print("Searching file: ", file_name) + text_file = open(file_name) + content = text_file.read() + regex_rule = re.compile(search_rule) + res = regex_rule.findall(content) + print(res) + except FileNotFoundError as e: + print("FileNotFoundError: " + str(e)) + except PermissionError: + print("PermissionError") + + +if __name__ == "__main__": + regex_search() diff --git a/students/piatkowska_anna/lesson_09_files/text_file.txt b/students/piatkowska_anna/lesson_09_files/text_file.txt new file mode 100644 index 000000000..f6739f2bd --- /dev/null +++ b/students/piatkowska_anna/lesson_09_files/text_file.txt @@ -0,0 +1,2 @@ +The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was +unaffected by these events. \ No newline at end of file