-
Notifications
You must be signed in to change notification settings - Fork 72
Zamber/lesson 09 files #718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gubrynka
wants to merge
2
commits into
jedzej:master
Choose a base branch
from
gubrynka:zamber/lesson_09_files
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
students/piatkowska_anna/lesson_09_files/extending_the_multiclipboard.pyw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| ''' | ||
| Extending the Multiclipboard | ||
| Extend the multiclipboard program in this chapter | ||
| so that it has a delete <keyword> 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 <keyword> - Saves clipboard to keyword. | ||
| # py.exe mcb.pyw <keyword> - Loads keyword to clipboard. | ||
| # py.exe mcb.pyw list - Loads all keywords to clipboard. | ||
| # py.exe mcb.pyw delete <keyword> - 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <text_file_name.txt> - 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <regular_expresion> <dir_path> | ||
| # <regular_expresion> - specifies regular expression, | ||
| # <dir_path> - 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 <regular_expresion> <dir_path>") | ||
| print("<regular_expresion> - specifies regular expression,") | ||
| print("<dir_path> - 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. F821 undefined name 'PermissionError' |
||
| print("PermissionError") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| regex_search() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was | ||
| unaffected by these events. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
F821 undefined name 'FileNotFoundError'