Checking if dictionary contains the date and plan
Question: So I am trying to create a function that checks whether or not the contents of
a function is true:

    
    
    def command_add(date, event, calendar):
    
        '''
        Add event_details to the list at calendar[date]
        Create date if it was not there
    
        :param date: A string  as "YYYY-MM-DD"
        :param event: A string describing the event
        :param calendar: The calendars database
        :return: a string indicating any errors, "" for no errors
        '''
    >>> calendar == {'2015-10-20': ['Python']}
    True
    >>> command_add("2015-11-01", "Computer Science class", calendar)
    ''
    

How would I write such a function? The problem I'm having is how to make the
string or how to see if the string for the date is in the format 'YYYY-MM-DD'.


Answer: The following code uses `strptime` to parse the date, if the parsing fails it
is not a proper date string. Then it checks if the date is already in the
calendar dict or not to see whether to append or add the first entry.

    
    
    from datetime import datetime
    
    def command_add(date, event, calendar):
    
        '''
        Add event_details to the list at calendar[date]
        Create date if it was not there
    
        :param date: A string  as "YYYY-MM-DD"
        :param event: A string describing the event
        :param calendar: The calendars database
        :return: a string indicating any errors, "" for no errors
        '''
        try:
            datetime.strptime('%Y-%m-%d', date):
        except ValueError:
            return 'Error parsing date'
        else:
            if date in calendar:
                calendar[date].append(event)
            else:
                calendar[date] = [event]
    
        return ''
    

