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

MCS 275 Spring 2023 Worksheet 14

  • Course instructor: Emily Dumas

Topics

This worksheet focuses on Flask web applications and anonymous functions.

Resources

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.

1. Get OrderNova or TrackFlow running locally

The Flask application written in lecture was called OrderNova in the 12pm section, and TrackFlow in the 1pm section. The applications are quite similar, and use a SQLite database to manage a queue of work orders that can be created, assigned to workers, and marked as completed.

To prepare for the next problem, download a copy of the Flask application for your section and get it running on your computer.

A. Download

The application has a number of files, so you can do it in either of these ways:

  • Download and extract the ZIP file linked below, which contains a snapshot of the application as it was on April 18, 2023.
  • Or if you prefer you can download the files one by one from the course sample code repository (see below for the list of files you need)

Note: There was an issue in timefmt.py that caused the application to return 500 Internal Server Error messages on some versions of Windows. I fixed this in an update on April 18. Thanks to Johnny Joyce for finding the cause of this problem.

List of files needed to run TrackFlow

  • trackflow.py - Main program
  • createdb.py - Utility to reset database
  • timefmt.py - Module for time formatting
  • static/ - Subdirectory for static files
  • static/trackflow.css - Main stylesheet
  • templates/ - Subdirectory for templates
  • templates/tf-showorder.html - Template for displaying work order status
  • templates/tf-workerview.html - Template for displaying worker view
  • templates/tf-neworder.html - Template for new work order form

List of files needed to run OrderNova

  • ordernova.py - Main program
  • createdb.py - Utility to reset database
  • timefmt.py - Module for time formatting
  • static/ - Subdirectory for static files
  • static/ordernova.css - Main stylesheet
  • templates/ - Subdirectory for templates
  • templates/on-showorder.html - Template for displaying work order status
  • templates/on-workerview.html - Template for displaying worker view
  • templates/on-neworder.html - Template for new work order form

B. Test it out

Open a terminal and cd to the location of the main .py script (ordernova.py or trackflow.py). Run that script with Python, e.g. using a command

python3 ordernova.py

or similarly with your interpreter name and possibly replacing ordernova with trackflow.

You should see output similar to this:

The database 'ordernova.sqlite' was not found.  Creating it.
Making sure the DB contains the necessary tables...Done
Populating DB with sample data, since it was empty...Done
 * Serving Flask app 'OrderNova'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit

Notice the port number 5000 on the second-to-last line. It might not be 5000 for you, but you need to know it.

Now, open a browser and visit http://localhost:5000/, replacing 5000 with the port number as needed.

You should see a simple white home page with links to some of the application endpoints. Try them out. Create a work order. Take it on as an assignment. Complete it.

2. New features

Modify the work order tracking application to add these new features. Test them to make sure they work.

A. Workers list

This application doesn't have a table to store workers, but you can generate a list of workers that have appeared as the assigned worker for some task in the table (whether completed or not).

Make a route /workerlist/ that generates a page styled similarly to the work order view page, but which gives a bulleted list of all the workers currently in the database. Each worker's name should only appear once. The worker's username should be a link to their worker view page.

So for example you might see a list like this:

Workers

  • ddumas
  • gwashington


And the first item would link to /worker/ddumas/, and the next one to /worker/gwashington/

B. On this day

Here's a function that can take a day described by its year, month, and day numbers and return two time stamps ts0,ts1 so that a timestamp x lies within that day if and only if ts0 <= x < ts1.

In [29]:
import datetime

def timestamp_range_for_day(year,month,day):
    """
    Return the timestamps when a calendar day
    begins and ends.
    """
    ts0 = datetime.datetime(year,month,day).timestamp()
    ts1 = (datetime.datetime(year,month,day)+datetime.timedelta(days=1)).timestamp()
    return ts0,ts1

Use this to add the following feature to the work order tracking application: Visiting a URL of the form /reports/day/2023/04/15/ will show a page listing the work orders created on that day, assigned on that day, and completed on that day. These should be in three separate sections, and modeled after the worker view template.

3. Custom sorts and max/min

Use anonymous functions (lambda) with Python's sorted(), max(), or min() functions to answer these questions.

A.

Generate the first 20 powers of 7 (meaning $7^1$ to $7^{20}$) and sort them according to how many different decimal digits they use. That is, 343 would appear near the start of the list since it uses only two distinct digits, while 1628413597910449 would appear near the end, as it uses all 10 digits.

B.

Here's a text file with 10000 English words, one per line:

Let's say the endscore of a word is the number of times its last letter appears in the word. For example, "plasma" has an endscore of 2 because the last letter, a, appears twice. And "associates" has an endscope of 3 because it contains 3 copies of the last letter, s.

Using custom sorting and lambda, find 20 words with the highest endscores in this list.

C.

Use the same word list as the last problem. Suppose we say the variety of a word is the ratio of the number of distinct letters to the length of the word. For example, "cameras" has 6 distinct letters and has a length of 7, so its variety is $\frac67 \approx 0.85714$.

Among words with at least four letters, find one that has the lowest variety.