Paul's Python CoursePPC

Project 9 of 20, for Caitlin

Opening Hours Module

One module of opening-hours helpers, imported by every floor of the Clocktower, and Caitlin's board by the front door to show it off.

About 60 minutes, in 12 steps.

Sign up free to open itLog in

Act 2 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • CaitlinI want a board by the front door that shows whether every floor is open or shut right now.
  • CaitlinThe trouble is every floor has its own copy of the opening-hours code.
  • CaitlinSomebody changed the Workshop's copy and now it closes at half past never.
  • PaulOkay, that somebody was me. Let's put it in a module, write it once and import it everywhere.

Write hours.py, a module every floor can import. A room's hours are a dictionary of day to (opens, closes), in whole hours on the 24-hour clock. A day that's missing is a day the room is closed.

  • is_open(hours, when): True if the room is open at when, a datetime. Open from the opening hour; closed from the closing hour.
  • sign(hours): seven lines for the door, Monday first: Mon 8 till 18, or Sun closed.
  • busiest_day(visits): the day name, like Sat, that comes up most in a list of visit times. If days tie, the one seen first. No visits: None.

The board for the front door, main.py, is already written. Run on its own, hours.py prints a quick check of itself; imported, it stays quiet.

What it prints
Saturday 11:30 at the Clocktower
The Studio: open
The Makerspace: closed
The Workshop: open

The Studio's door:
  Mon 8 till 18
  Tue 8 till 18
  Wed 8 till 18
  Thu 8 till 18
  Fri 8 till 18
  Sat 8 till 13
  Sun closed

Busiest in the Studio: Sat

What you learn

What's in it

  1. Ready-made modules
  2. Sunday, and a bit
  3. The shelf of ready-made modules
  4. A module of your own
  5. Same name, two jobs
  6. Only when it's run itself
  7. What's my name
  8. How Paul would do it
  9. Fix Riya's version
  10. Make the hours module
  11. Ship it