'How to fix "AttributeError: type object has no attribute" in python?

I am adding some code to the preset code to check the time availability, which is if the meeting time can fit into the proposed time schedule. However, I keep getting the following error. Can anyone please give me some advices? Thanks so much for your time.

Preset codes:

from datetime import datetime
class Meeting:
    def __init__(self, start_time, end_time):
        self.start_time = start_time
        self.end_time = end_time

My codes:

def check_availability(meetings, proposed_time): 
    meeting_start = Meeting.datetime.start_time.hour 
    meeting_end = Meeting.datetime.end_time.hour  
    ok_time = datetime.proposed_time.hour   
    if meeting_start < ok_time < meeting_end:
        return True 
    else:
        return False 

meetings = [Meeting(datetime(2018, 8, 1, 9, 0, 0), datetime(2018, 8, 1, 11, 
0, 0)), Meeting(datetime(2018, 8, 1, 15, 0, 0), datetime(2018, 8, 1, 16, 0, 
0)), Meeting(datetime(2018, 8, 2, 9, 0, 0), datetime(2018, 8, 2, 10, 0, 0))]

print(check_availability(meetings, datetime(2018, 8, 1, 12, 0, 0)))
print(check_availability(meetings, datetime(2018, 8, 1, 10, 0, 0)))


Solution 1:[1]

Your code raises this exception:

AttributeError: type object 'Meeting' has no attribute 'datetime'

At this line:

meeting_start = Meeting.datetime.start_time.hour

Python is telling you that the Meeting class doesn't have an attribute named datetime. This is true: the Meeting class is a factory for making meeting objects (or instances), and these objects have start_time and end_time attributes, which are set by passing datetime instances to Meeting's __init__ method. These attributes can be accessed like this:

>>> meeting = Meeting(datetime(2018, 8, 1, 9, 0, 0), datetime(2018, 8, 1, 11, 
0, 0))
>>> print(meeting.start_time)
2018-08-01 09:00:00                                                                                                     
>>> print(meeting.end_time)                                                                                             
2018-08-01 11:00:00 

Your check_availability function is being passed a list of meetings, so you need to loop over the list to check whether any of the meetings conflict with the proposed meeting time.

def check_availability(meetings, proposed_time):
    # Loop over the list of meetings; "meeting"
    # is the meeting that you are currently inspecting.
    for meeting in meetings:
        # if proposed_time is between meeting.start_time
        # and meeting.end_time, return False
    # If you get through the list without returning False
    # then the proposed time must be ok, so return True.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1