You can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
You can only take this quiz today.
There is not makeup for the weekly quiz because Gradescope does not permit it.
I have posted a solution to homework 5 here.
Let's take a look.
I have posted homework 7 here.
It is NOT due this coming Sunday.
Instead it is due Sunday, March 24th.
This is to give you time to study for the midterm.
And to give me time to score it.
The Midterm exam for this course will be held on Tuesday, March 19th.
That is the first Tuesday after the Spring Break.
The exam will be given in this room.
It will consist of questions like those on the quizzes along with questions asking you to write short segments of Python code.
60% of the points on this exam will consist of questions from the Ungraded Class Quizzes.
There will be 15 of these questions worth 4 points each.
The other 40% of points will come from four questions that ask you to write a short segment of code.
Each of the code questions is worth 10 points each.
To study for the code questions you should know
A good way to study for the code questions is to review the Class Exercises and homework solutions.
The last class before the exam, Thursday, March 7th, will be a review session.
You will only be responsible for the material in the Class Notes for that class on the exam.
You will find the Midterm review Class Notes here.
If for some reason you cannot take the exam on the date mentioned above you must contact me to make alternate arrangements.
The Midterm is given on paper.
I scan each exam paper and upload the scans to Gradescope.
I score the exam on Gradescope.
You will get an email from Gradescope with your score when I am done.
The Midterm is a closed book exam.
You are not allowed to use use any resource, other than what is in your head, while taking the exam.
Cheating on the exam will result in a score of 0 and will be reported to the Administration.
Remember your Oath of Honesty.
To prevent cheating, certain rules will be enforced during the exam.
The new material in today's class will not appear on the Midterm.
You will only be responsible for the material in the Class Notes for the next class.
They will cover Class Notes 2 through 12.
There will be no graded quiz next week.
Are there any questions before I begin?
205.236.184.72 - - [09/Mar/2014:00:03:21 +0000] "GET /wzbc-2014-03-05-14-00.mp3 HTTP/1.1" 200 56810323
205.236.184.101 - - [09/Mar/2014:00:03:21 +0000] "GET /wzbc-2014-03-05-14-00.mp3 HTTP/1.1" 200 56810323
\d+\.\d+\.\d+\.\d+.*GET
(\d+\.\d+\.\d+\.\d+).*GET
>>> pattern_object = re.compile("(\d+\.\d+\.\d+\.\d+).*GET")
for
loop ...>>> match_object = pattern_object.search('205.236.184.101 - - [09/Mar/2014:00:03:21 +0000] "GET /wzbc-2014-03-05-14-00.mp3 HTTP/1.1" 200 56810323')
>>> match_object <_sre.SRE_Match object; span=(0, 53), match="205.236.184.101 - - [09/Mar/2014:00:03:21 +0000]which means we have a match
>>> match_object.group(1) '205.236.184.101'
#! /usr/bin/python3 # tests regular expressions and returns # the first group if it can import re import os.path import sys def regex_match_with_group(regular_expression, line): pattern_object = re.compile(regular_expression) match_object = pattern_object.search(line) if match_object : try : return_string = match_object.group(1) print("regular expression:", regular_expression) print("matches:", line) print("returns:", return_string) except : print("Match found but no substring returned") else: print("No match") if len(sys.argv) < 3 : print("Usage: ", os.path.basename(sys.argv[0]), " REGULAR_EXPRESSION STRING_TO_MATCH") sys.exit() regex = sys.argv[1] line = sys.argv[2] regex_match_with_group(regex, line)
try/except
statement?
$ ./regex_test_with_group.py "(\d\d\d)" abc123456789 regular expression: (\d\d\d) matches: abc123456789 returns: 123
$ ./regex_test_with_group.py "(\d{5})" 123456789 regular expression: (\d{5}) matches: 123456789 returns: 12345
$ ./regex_test_with_group.py "\D*(\d{2,5})" "asdasd 12 kxhc" regular expression: \D*(\d{2,5}) matches: asdasd 12 kxhc returns: 12 $ ./regex_test_with_group.py "\D*(\d{2,5})" "---12345---------" regular expression: \D*(\d{2,5}) matches: ---12345--------- returns: 12345
$ ./regex_test_with_group.py "([abc])" bdewrosdf regular expression: ([abc]) matches: bdewrosdf returns: b
$ ./regex_test_with_group.py "([abc]+)" bcaewrosdf regular expression: ([abc]+) matches: bcaewrosdf returns: bca
[abcdefghijklmnopqrstuvwxyz]
$ ./regex_test_with_group.py "\W*([a-d]+)" ---------bacdnmonpn-------- regular expression: \W*([a-d]+) matches: ---------bacdnmonpn-------- returns: bacd
$ ./regex_test_with_group.py "^(\d)" "123456789" regular expression: ^(\d) matches: 123456789 returns: 1 $ ./regex_test_with_group.py "^(\d)" "a123456789" No match
$ ./regex_test_with_group.py "(\d)$" "a123456789" regular expression: (\d)$ matches: a123456789 returns: 9 $ ./regex_test_with_group.py "(\d)$" "a123456789-" No match
$ ./regex_test_with_group.py "([5-9]+)" 987654321 regular expression: ([5-9]+) matches: 987654321 returns: 98765
$ ./regex_test_with_group.py "([^5-9]+)" asdfasd123456789 regular expression: ([^5-9]+) matches: asdfasd123456789 returns: asdfasd1234
$ ./regex_test_with_group.py "(Red|Blue)" "Red Sox" regular expression: (Red|Blue) matches: Red Sox returns: Red $ ./regex_test_with_group.py ".*(Sox|Ducks)" "Red Sox" regular expression: .*(Sox|Ducks) matches: Red Sox returns: Sox
<td>Class 4</td> <td>February 6th</td>
$ ./regex_test_with_group.py "<td>(.*)</td>" "<td>Class 4</td> <td>February 6th</td>" regular expression: <td>(.*)</td> matches: <td>Class 4</td> <td>February 6th</td> returns: Class 4</td> <td>February 6th
$ ./regex_test_with_group.py "<td>(.*?)</td>" "<td>Class 4</td> <td>February 6th</td>" regular expression: <td>(.*?)</td> matches: <td>Class 4</td> <td>February 6th</td> returns: Class 4
cp: cannot stat '/courses/it117/s17/ghoffman/jsmith/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/wombat/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/davidj/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/harry/hw/hw6/hw6.py': No such file or directory
sys
module
import sys
def open_file_read(filename): try: file = open(filename, "r") except: print("Cannot open", filename) sys.exit() else: return file
file1 = open_file_read("_submissions_missing.txt") for line in file1: print(line.strip()) print() file2 = open_file_read("xxxx")
$ ./email_missing.py cp: cannot stat '/courses/it117/s17/ghoffman/jsmith/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/wombat/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/davidj/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/harry/hw/hw6/hw6.py': No such file or directory Cannot open xxxx
cp: cannot stat
import re
pattern = re.compile("cp: cannot stat")
file = open_file_read("_submissions_missing.txt")
for line in file: match = pattern.search(line)
if match: print(line.strip())
$ ./email_missing.py cp: cannot stat '/courses/it117/s17/ghoffman/jsmith/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/wombat/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/davidj/hw/hw6/hw6.py': No such file or directory cp: cannot stat '/courses/it117/s17/ghoffman/harry/hw/hw6/hw6.py': No such file or directory
/courses/it117/s17/ghoffman/jsmith/hw/hw6/hw6.py
pattern = re.compile("cp: cannot stat.*ghoffman/(.*)/hw/")
for
loop is
for line in file: match = pattern.match(line) if match: username = match.group(1) print(username)
$ ./email_missing.py jsmith wombat davidj harry
pattern = re.compile("cp: cannot stat.*ghoffman/(.*)/hw/hw\d{1,2}/(\w+\.py)")
for
loop is
for line in file: match = pattern.match(line) if match: username = match.group(1) filename = match.group(2) print(username, filename)
$ ./email_missing.py jsmith hw6.py wombat hw6.py davidj hw6.py harry hw6.py
def get_username_filename(line): pattern = re.compile("cp: cannot stat.*ghoffman/(.*)/hw/hw\d{1,2}/(\w+\.py)") match = pattern.match(line) if match: username = match.group(1) filename = match.group(2) return username, filename else: return "",""
for line in file: username, filename = get_username_filename(line) print(username, filename)
$ ./email_missing_5.py jsmith hw6.py wombat hw6.py davidj hw6.py harry hw6.py
mail
command to do this ...mail
with the
-s option
mail
expects the user to type the rest of the message on the
command line
mail -s "Test message" Glenn.Hoffman@umb.edu < message.txt
def email_message(subject, email, filename): mail_command = "mail -s " + subject + " " + email + " < " + filename os.system(mail_command) print("Mail sent to " + email)
def mesage_text(): msg = "My collection scripts cannot find this file.\n" msg += "Please DO NOT respond to this message.\n" msg += "Simply put the missing file in the correct directory.\n" msg += "DO NOT notify me when the file is in the correct place.\n" msg += "I will collect all files later in the week.\n" msg += "\nGlenn" return msg
def open_file_write(filename): try: file = open(filename, "w") except: print("Could not open", filename) sys.exit() error_exit("Could not open file: " + filename) else: return file
msg_filename = "message.txt" msg_file = open_file_write(msg_filename) msg_file.write(mesage_text()) msg_file.close() file = open_file_read("_submissions_missing.txt") for line in file: username, filename = get_username_filename(line) email_message("Cannot find "+ filename, username + "@cs.umb.edu", msg_filename) os.remove(msg_filename)
import re import sys # opens a file for reading if it can def open_file_read(filename): try: file = open(filename, "r") except: print("Cannot open", filename) sys.exit() else: return file # opens a file for writing def open_file_write(filename): try: file = open(filename, "w") except: print("Could not open", filename) sys.exit() error_exit("Could not open file: " + filename) else: return file # send an email message using a message file def email_message(subject, email, filename): mail_command = "mail -s " + subject + " " + email + " < " + filename os.system(mail_command) print("Mail sent to " + email)
import utilities
file = utilities.open_file_read(filename)
from utilities import *
file = open_file_read(filename)
IT 117: Intermediate Scripting - Scoring Sheet Homework 4: hw4.py ======================================================= Student name: John Jones Has hashbang line 10 points Has correct permissions 10 points Has meaningful comment at top of script file 10 points Function comments meaningful 0 points no function comments Script runs without syntax errors 20 points word_frequencies_create has functioning try/except statement to catch file errors 10 points word_frequencies_create works properly 20 points word_frequency_print works properly 10 points
import os.path import sys import re from utilities import *
if len(sys.argv) < 2: print("Usage:", os.path.basename(sys.argv[0]), "SCORING_SHEET_FILENAME") sys.exit()
filename = sys.argv[1]
file = open_file_read(filename)
for line in file: print(line.strip())
\D*(\d{1,2})\s*points
for
loop
pattern = re.compile("(\d{1,2})\s*points")
for line in file: match = pattern(line) if match: print(line.strip())
for
loop
total = 0
for line in file: match = pattern.match(line) if match: score = int(match.group(1)) total += score
filename = sys.argv[1] file = open_file_read(filename) new_filename = filename + ".new" new_file = open_file_write(new_filename) pattern = re.compile("(\d{1,2})\s*points") total = 0 for line in file: if "Score" not in line: new_file.write(line) match = pattern(line) if match: score = int(match.group(1)) total += score new_file.write("Score: " + str(total))
file.close() new_file.close()
os.system("mv " + new_filename + " " + filename)