A document from MCS 275 Spring 2022, instructor David Dumas. You can also get the notebook file.

MCS 275 Spring 2022 Homework 12

  • Course Instructor: David Dumas

Instructions:

  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)

Deadline

This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 12 April 2022.

Collaboration

Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

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.

Point distribution

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.

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( No problem 1 as usual )

Problem 2

Tutorial: How to list files and get modification time

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.

In [10]:
import os

for fn in os.listdir():
    print("Filename={}\nLastModified={}\n".format(fn,int(os.path.getmtime(fn))))
Filename=homework11soln.ipynb
LastModified=1649258369

Filename=homework12.ipynb
LastModified=1649357087

Filename=homework10.ipynb
LastModified=1647552323

Filename=homework11.ipynb
LastModified=1647381255

Filename=homework10soln.ipynb
LastModified=1648583672

The actual task

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:

  • Open a SQLite database file called lastmod.db
  • Create a table called files as follows if it doesn't already exist:
    • Column name of type TEXT (i.e. a Python string)
    • Column lastmod of type INTEGER
    • Column revcount of type INTEGER
  • Look at every file in the current directory, and do the following for each one:
    • If the filename is lastmod.db, just continue to the next iteration of the loop (skipping everything below for this file)
    • If there is already a row in the table files that has the same name as this file, then:
      • Check whether the lastmod value for that row is equal to the last modified time of this file.
        • If they are the same: Do nothing
        • If they are different: Update the lastmod value for that row to the last modified time of this file, and also increase the value of revcount in that row by 1
    • Otherwise (i.e. if there is no row in the table files that has the same name as this file):
      • Create a new row in table files using this file's name as name, using its last modified time as lastmod, and setting revcount to 1

This 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.

Problem 3

Show that you've read the feedback on Project 3

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.

Optional: Ask questions

If you have questions about your project 3 feedback, the solution, or anything else, feel free to include them in hwk12prob3.txt.