.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 12 April 2022.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.
This homework assignment has two problems. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.
Ask your instructor or TA a question by email, in office hours, or on discord.
For use in the rest of this problem, here's a demonstration of how to list all the files in the current directory, and to get the time (in seconds in Jan 1 1970) when the file was most recently changed. You can find more about os.listdir
and os.getmtime
in the documentation of the os
module.
import os
for fn in os.listdir():
print("Filename={}\nLastModified={}\n".format(fn,int(os.path.getmtime(fn))))
Now, using the code above as a starting point, write a new program called hwk12prob2.py
that examines files in the current directory and stores info about their last modified times and how often they've changed in a database.
Specifically, the program must do the following:
lastmod.db
files
as follows if it doesn't already exist:name
of type TEXT (i.e. a Python string)lastmod
of type INTEGERrevcount
of type INTEGERlastmod.db
, just continue to the next iteration of the loop (skipping everything below for this file)files
that has the same name
as this file, then:lastmod
value for that row is equal to the last modified time of this file.lastmod
value for that row to the last modified time of this file, and also increase the value of revcount
in that row by 1files
that has the same name
as this file):files
using this file's name as name
, using its last modified time as lastmod
, and setting revcount
to 1This means that if you run hwk12prob2.py
in a given directory on a regular basis (say, every hour or every few minutes), it will notice every time a file has changed and keep a count of how many different versions it has seen thus far. The database will also track the most recent modification time of every file, including any that have since been deleted.
Test your program by running it in the directory where you're working on your assignment. Make a modification to a file in that directory, and run hwk12prob2.py
again. Now if you open lastmod.db
in the SQLite command line shell and run SELECT * FROM files;
it should show all the files, most of which will have revcount
equal to 1, but the file you modified should have revcount
equal to 2.
import sqlite3
import os
con = sqlite3.connect("lastmod.db")
con.execute("CREATE TABLE IF NOT EXISTS files (name TEXT, lastmod INT, revcount INT);")
for filename in os.listdir(): # Iterate over every file
if filename == "lastmod.db": # Skip database file itself
continue
c = con.execute("SELECT * FROM files WHERE name = ?;", (filename,))
row = c.fetchone()
if row is not None: # If row is already in table
lastmod_os = os.path.getmtime(filename) # Last modification according to OS
lastmod_db = row[1] # Last modification according to database
revcount = row[2]
if int(lastmod_os) != int(lastmod_db):
con.execute("UPDATE files SET lastmod = ?, revcount = ? WHERE name = ?;", (int(lastmod_os), revcount+1, filename))
# Print statement not required by question, but is helpful debugging or user experience.
print("Updated row for file {}. Previous lastmod: {}. New lastmod: {}".format(filename, int(lastmod_db), int(lastmod_os)))
else: # If row is not in table
lastmod_os = os.path.getmtime(filename) # last modification according to OS
con.execute("INSERT INTO files VALUES (?, ?, ?);", (filename, int(lastmod_os), 1))
con.commit()
con.close()
Note from Johnny: This was run in a folder where I put a bunch of my MCS 275 stuff 🙂 Notice that homework12soln.ipynb
(i.e. this file you're currently reading) has revcount
higher than 1 because I intentionally saved and re-ran it many times.
import sqlite3
con = sqlite3.connect("lastmod.db")
c = con.execute("SELECT * FROM files;")
for row in c:
print(row)
con.close()
By now you've received and read your graded Project 3 submission (the PDF shared with you on google drive, for which I also emailed you a link on 3 April). Hopefully, you have also read through the solution I posted.
As a way to give you some credit for reading the feedback on Project 3, please answer this question:
At the bottom of the last or second to last page of the PDF of your Project 3 feedback, I included a small drawing labeled "This is just a drawing, not a comment on your work". Please describe that drawing briefly.
(You don't need to be very detailed, e.g. "three green triangles" or "an orange cube" would be enough detail.)
Put your answer in a text file called hwk12prob3.txt
and upload it to Gradescope.
If you have questions about your project 3 feedback, the solution, or anything else, feel free to include them in hwk12prob3.txt
.