Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
82 changes: 82 additions & 0 deletions students/piatkowska_anna/lesson_09_files/mad_libs.py
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()
54 changes: 54 additions & 0 deletions students/piatkowska_anna/lesson_09_files/regex_search.py
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F821 undefined name 'FileNotFoundError'

print("FileNotFoundError: " + str(e))
except PermissionError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F821 undefined name 'PermissionError'

print("PermissionError")


if __name__ == "__main__":
regex_search()
2 changes: 2 additions & 0 deletions students/piatkowska_anna/lesson_09_files/text_file.txt
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.