Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I keep getting the following error from the below functions: The call str _ to _ time ( ' 2 0 1 6 - 0

I keep getting the following error from the below functions: The call str_to_time('2016-04-15', None) returns None, not datetime.datetime(2016,4,15,0,0).
import pytz
from dateutil import parser
def str_to_time(timestamp,tzsource=None):
"""
Returns the datetime object for the given timestamp (or None if timestamp is
invalid).
This function should just use the parse function in dateutil.parser to
convert the timestamp to a datetime object. If it is not a valid date (so
the parser crashes), this function should return None.
If the timestamp has a time zone, then it should keep that time zone even if
the value for tzsource is not None. Otherwise, if timestamp has no time zone
and tzsource is not None, then this function will use tzsource to assign
a time zone to the new datetime object.
The value for tzsource can be None, a string, or a datetime object. If it
is a string, it will be the name of a time zone, and it should localize the
timestamp. If it is another datetime, then the datetime object created from
timestamp should get the same time zone as tzsource.
Parameter timestamp: The time stamp to convert
Precondition: timestamp is a string
Parameter tzsource: The time zone to use (OPTIONAL)
Precondition: tzsource is either None, a string naming a valid time zone,
or a datetime object.
"""
# HINT: Use the code from the previous exercise and add time zone handling.
# Use localize if tzsource is a string; otherwise replace the time zone if not None
try:
time = parser.parse(timestamp)
except ValueError:
return None
if time.tzinfo is not None:
return time
if tzsource is None:
return None
if isinstance(tzsource, str):
tz = pytz.timezone(tzsource)
time = tz.localize(time)
else:
time = time.replace(tzinfo=tzsource.tzinfo)
return time
pass
def daytime(time,daycycle):
"""
Returns true if the time takes place during the day.
A time is during the day if it is after sunrise but before sunset, as
indicated by the daycycle dicitionary.
A daycycle dictionary has keys for several years (as int). 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 from this set. 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)
try:
date_key = f"{time.month:02d}-{time.day:02d}"
year_key = str(time.year)
tz = pytz.timezone(daycycle['timezone'])
if time.tzinfo is None:
time = tz.localize(time)
sunrise = str_to_time(f"{year_key}-{date_key}{daycycle[year_key][date_key]['sunrise']}", tz)
sunset = str_to_time(f"{year_key}-{date_key}{daycycle[year_key][date_key]['sunset']}", tz)
if sunrise is None or sunset is None:
return None
return sunrise <= time < sunset
except:
return None
pass

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

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

19. LO.5 What funding vehicles are available for a Keogh plan?

Answered: 1 week ago

Question

=+v3. Determine if they are targeting the same audience.

Answered: 1 week ago

Question

=+1. Compare the copy on both sites. Are they alike or distinctive?

Answered: 1 week ago

Question

=+What kind of clients would work well in this medium?

Answered: 1 week ago