Question: from dateutil import parser from dateutil import tz import datetime import pytz def daytime ( time , daycycle ) : Returns True
from dateutil import parser
from dateutil import tz
import datetime
import pytz
def daytimetimedaycycle:
Returns True if the time takes place during the day, False otherwise and
returns None if a key does not exist in the dictionary
A time is during the day if it is after sunrise but before sunset, as
indicated by the daycycle dictionary.
A daycycle dictionary has keys for several years as strings The value for
each year is also a dictionary, taking strings of the form mmdd The
value for that key is a THIRD dictionary, with two keys "sunrise" and
"sunset". The value for each of those two keys is a string in hour
time format.
For example, here is what part of a daycycle dictionary might look like:
:
:
"sunrise": :
"sunset": :
:
"sunrise": :
"sunset": :
In addition, the daycycle dictionary has a key 'timezone' that expresses the
timezone as a string. This function uses that timezone when constructing
datetime objects using data from the daycycle dictionary. Also, if the time
parameter does not have a timezone, we assume that it is in the same timezone
as the daycycle dictionary.
Parameter time: The time to check
Precondition: time is a datetime object
Parameter daycycle: The daycycle dictionary
Precondition: daycycle is a valid daycycle dictionary, as described above
# HINT: Use the code from the previous exercise to get sunset AND sunrise
# Add a timezone to time if one is missing the one from the daycycle
# Add timezone to time if one is missing
year strtimeyear
if year in daycycle:
monthday :d:dformattimemonth, time.day
if monthday in daycycleyear:
sunrise daycycleyearmonthdaysunrise
sunset daycycleyearmonthdaysunset
sunrisetime datetime.datetime.strptimesunriseH:Mtime
sunsettime datetime.datetime.strptimesunsetH:Mtime
return sunrisetime time.time sunsettime Errror "The call daytimedatetimedatetime tzinfodaycycle returns False, not True." tests: def testdaytime:
Test procedure for the function daytime
printTesting daytime
# Find the directory with this file in it
parent ospath.splitfile
filepath ospath.joinparent'daycycle.json'
file openfilepath
daycycle json.loadsfileread
file.close
times T::True,TrueT::True,True
T::False,TrueT::True,False
T::True,TrueT::False,False
T::False,TrueT::True,True
T::True,TrueT::False,False
T::False,TrueT::False,False
T::True,TrueT::True,True
T::True,TrueT::False,False
T::True,TrueT::False,False
# CHECK THE TEST CASES
for time in times:
act funcs.strtotimetime"AmericaNewYork"
day funcs.daytimeactdaycycle
introcs.assertequalstime day
act funcs.strtotimetime"AmericaChicago
day funcs.daytimeactdaycycle
introcs.assertequalstime day
# Test the case of no time zone Same result as first test above.Added by STT
act funcs.strtotimetime
day funcs.daytimeactdaycycle
introcs.assertequalstime day
if namemain:
teststrtotime
testdaytime
printModule funcs passed all tests.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
