Modules and imports
Read the task files below or download the starter files. Use the Canvas assignment to open CodeGrade and submit your work.
Classify departures using a module
A taxi booking program needs to classify a departure time as "peak" or "off-peak". The file time_tools.py contains complete versions of the time functions from the earlier exercises. Both functions are supplied with this question.
The project has the following files. The two Python files are in the same directory.
Listing 1 shows the project files.
Supplied functions
time_tools.py is a module: a Python file whose functions another file can import. You can open it in the editor to read its complete code. Keep this supplied file unchanged.
time_to_minutes(time_text) takes a time in HH:MM format and returns the number of minutes since midnight. Hours must be from 00 to 23, and minutes from 00 to 59. It returns None for an invalid input. For example, time_to_minutes("08:30") returns 510, and time_to_minutes("25:00") returns None.
classify_peak_hours(minutes) returns "peak" from 07:00 up to, but excluding, 09:00 and from 17:00 up to, but excluding, 19:00. It returns "off-peak" for the other valid minutes of the day and None for an invalid input, including None. For example, classify_peak_hours(510) returns "peak", and classify_peak_hours(540) returns "off-peak".
Your task
Complete departure_period(time_text) in departure_period.py.
- Import the supplied module, or import its two functions by name.
- Use
time_to_minutesto converttime_text, then useclassify_peak_hoursto classify the result. - Return the classification. Return
Nonewhen the time is invalid. Return the value without printing it.
Use the supplied functions for both operations. Keep their implementations in time_tools.py; do not copy their bodies into your answer. Both import module with qualified calls and from module import function with named calls are accepted. Inputs are strings containing ASCII characters, integers, or None. No earlier submission is required.
Use these calls to check your function.
Table 1 lists the calls and their expected return values.
| Call | Expected return value |
|---|---|
departure_period("06:59") |
"off-peak" |
departure_period("07:00") |
"peak" |
departure_period("08:30") |
"peak" |
departure_period("09:00") |
"off-peak" |
departure_period("17:00") |
"peak" |
departure_period("19:00") |
"off-peak" |
departure_period("25:00") |
None |
departure_period(None) |
None |
In CodeGrade, read the two Python files, complete departure_period.py, run Auto Test, and select Hand-in your work. This local packet also runs in an ordinary Python project with both files in the same directory.
Python files
time_tools.py
Listing 2 shows the contents of time_tools.py.
time_tools.py
def time_to_minutes(time_str: str) -> int:
"""Convert a time string in HH:MM format to the total number of minutes.
Parameters
----------
time_str : str
A string representing time in 24-hour format (HH:MM).
Returns
-------
int or None
The total number of minutes represented by the input string, or None
if the input format is invalid or represents an invalid time.
"""
# Define constants for validation
HOURS_IN_A_DAY = 24 # Total hours in a day
MINUTES_IN_AN_HOUR = 60 # Total minutes in an hour
# Check if the input is a string
if not isinstance(time_str, str):
return None
# Check if the string is in the correct format
# (5 characters with ':' at index 2)
if len(time_str) != 5 or time_str[2] != ":":
return None
# Split the string into hours and minutes
hours, minutes = time_str.split(":", 1)
# Check if hours and minutes are digit strings
if not hours.isdigit() or not minutes.isdigit():
return None
# Convert hours and minutes to integers
hours, minutes = int(hours), int(minutes)
# Check if hours and minutes fall within valid ranges
# and calculate total minutes
if 0 <= hours < HOURS_IN_A_DAY and 0 <= minutes < MINUTES_IN_AN_HOUR:
return hours * MINUTES_IN_AN_HOUR + minutes
return None
def classify_peak_hours(minutes_since_midnight: int) -> str:
"""Classify a given time in minutes as 'peak' or 'off-peak'.
Peak hours are defined as [07:00-09:00) and [17:00-19:00).
Parameters
----------
minutes_since_midnight : int
The number of minutes elapsed since midnight.
Returns
-------
str or None
Returns "peak" if the time falls within peak hours, "off-peak" otherwise.
Returns None for invalid inputs.
"""
# Define constants
MINUTES_IN_A_DAY = 24 * 60 # Total minutes in a day (00:00 to 23:59)
# Validate the input is an integer
if not isinstance(minutes_since_midnight, int):
return None
# Validate the input is within the range of 0 to MINUTES_IN_A_DAY
if not (0 <= minutes_since_midnight < MINUTES_IN_A_DAY):
return None
# Define peak hour ranges in minutes
morning_peak_start = 7 * 60 # 07:00 in minutes
morning_peak_end = 9 * 60 # 09:00 in minutes
evening_peak_start = 17 * 60 # 17:00 in minutes
evening_peak_end = 19 * 60 # 19:00 in minutes
# Check if the time is within morning or evening peak hours
is_morning_peak = morning_peak_start <= minutes_since_midnight < morning_peak_end
is_evening_peak = evening_peak_start <= minutes_since_midnight < evening_peak_end
if is_morning_peak or is_evening_peak:
return "peak"
return "off-peak"departure_period.py
Listing 3 shows the contents of departure_period.py.
departure_period.py
def departure_period(time_text):
"""Classify a departure time using the supplied time_tools module.
Parameters
----------
time_text : str, int, or None
A time string containing ASCII characters, or an invalid input.
Returns
-------
str or None
"peak" or "off-peak" for a valid HH:MM time; None otherwise.
"""
pass