Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me fix the error in my python code? The tests are correct. Right now I am receiving the following error: The call

Can you help me fix the error in my python code? The tests are correct. Right now I am receiving the following error:

The call daytime(datetime.datetime(2015, 6, 5, 7, 0, tzinfo='US/Eastern' EDT-1 day, 20:00:00 DST>),daycycle) crashed. Traceback (most recent call last): File "/home/codio/workspace/.guides/tests/problem5/verifier.py", line 412, in grade_func2 received = func(input,daycycle) File "/home/codio/workspace/exercise5/funcs.py", line 126, in daytime if back_sunrise < time < back_sunset: TypeError: can't compare offset-naive and offset-aware datetimes 

This is the function:

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:

tz_cycle = daycycle['timezone']

if time.tzinfo is not None:

year1 = time.strftime('%Y')

date1 = time.strftime('%m-%d')

sunrisetime = daycycle[year1][date1]['sunrise']

sunsettime = daycycle[year1][date1]['sunset']

y = year1 + '-' + date1

y1 = datetime.datetime.strptime(y, '%Y-%m-%d')

y3 = datetime.datetime.strptime(sunrisetime, '%H:%M').time()

y2 = datetime.datetime.combine(y1, y3)

x = y2.isoformat()

y4 = datetime.datetime.strptime(sunsettime, '%H:%M').time()

y5 = datetime.datetime.combine(y1, y4)

x1 = y5.isoformat()

back_sunrise = str_to_time(x, tz_cycle)

back_sunset = str_to_time(x1, tz_cycle)

if back_sunrise < time < back_sunset:

return True

else:

return False

except KeyError:

return None

These are the tests:

def test_daytime():

"""

Test procedure for the function daytime()

"""

print('Testing daytime()')

# Find the directory with this file in it

parent = os.path.split(__file__)[0]

filepath = os.path.join(parent,'daycycle.json')

file = open(filepath)

daycycle = json.loads(file.read())

file.close()

times = [('2015-06-05T07:00:00',True,True),('2015-06-05T17:00:00',True,True),

('2015-10-31T06:00:00',False,True), ('2015-10-31T17:00:00',True,False),

('2015-11-17T07:00:00',True,True),('2015-11-17T17:00:00',False,False),

('2015-12-11T07:00:00',False,True), ('2015-06-05T17:00:00',True,True),

('2016-11-01T07:00:00',True,True),('2016-11-01T17:00:00',False,False),

('2017-11-17T07:00:00',False,True), ('2017-11-17T17:00:00',False,False),

('2018-06-05T07:00:00',True,True),('2018-06-05T17:00:00',True,True),

('2018-11-15T07:00:00',True,True),('2018-11-15T17:00:00',False,False),

('2019-11-15T07:00:00',True,True),('2019-11-15T17:00:00',False,False)]

# CHECK THE TEST CASES

for time in times:

act= funcs.str_to_time(time[0],"America/New_York")

day= funcs.daytime(act,daycycle)

introcs.assert_equals(time[1], day)

act= funcs.str_to_time(time[0],"America/Chicago")

day= funcs.daytime(act,daycycle)

introcs.assert_equals(time[2], day)

if __name__ == '__main__':

test_str_to_time()

test_daytime()

print('Module funcs passed all tests.')

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

Students also viewed these Programming questions

Question

Which of the key word is used to create an instance of a class?

Answered: 1 week ago

Question

How many bytes a char data type occupies?

Answered: 1 week ago

Question

The intermediate code is referred to as?

Answered: 1 week ago