Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 daytime(time,daycycle):
"""
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 'mm-dd'. 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 24-hour
time format.
For example, here is what part of a daycycle dictionary might look like:
"2015": {
"01-01": {
"sunrise": "07:35",
"sunset": "16:44"
},
"01-02": {
"sunrise": "07:36",
"sunset": "16:45"
},
...
}
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 = str(time.year)
if year in daycycle:
month_day ="{:02d}-{:02d}".format(time.month, time.day)
if month_day in daycycle[year]:
sunrise = daycycle[year][month_day]["sunrise"]
sunset = daycycle[year][month_day]["sunset"]
sunrise_time = datetime.datetime.strptime(sunrise,"%H:%M").time()
sunset_time = datetime.datetime.strptime(sunset,"%H:%M").time()
return sunrise_time time.time() sunset_time
return None Error:"The call daytime(datetime.datetime(2015,10,31,6,0, tzinfo=),daycycle) returns False, not True."Implement daytime
Read the specification of the function daytime in the module funcs.
Implement this function according to its specification.
This function is very similar to from the previous exercise, except
that it now asks you to determine whether a datetime object is between
sunrise and sunset (e.g., happened during the day). That means you will
need to extract both the sunrise and sunset this time and compare them
to given time value.
Note that the value may or may not have a time zone attached to it.
If it has a time zone attached, you must give both sunrise and sunset a
time zone in order to be able to compare them. The time zone is stored in
the daycycle dictionary using the key "timezone" (that value is a string). If
does not have a time zone, you can assume it has the same time
zone as sunrise and sunset.
When you have implemented the function, test your answer with the test
script. You should now pass all tests.
Check the Function
You may run this test multiple times.
LAST RUN on 2/10/2024,6:22:03 PM
The call daytime(datetime.datetime(2015,10,31,6,0, tzinfo=******cdots*******cdotscdots**
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 3 Lnai 8726

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448440, 978-3662448441

More Books

Students also viewed these Databases questions