dates

py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Feb 20, 2024

Type

py

Pages

3

Uploaded by MateClover22302

Report
""" File: dates.py Author: Nick Brobeck Course: CSC 120, Semester 2, 2024 Purpose: This program manages a database of dates and events. It allows users to insert events for specific dates, retrieve events for specific dates, and handle dates in different formats. """ class Date: """Class to represent a date with associated events.""" def __init__(self, date, event): """Initialize Date object. Parameters: date (str): The date in canonical format. event (str): The event associated with the date. """ self.date = date self.events = {event} def add_event(self, event): """Add an event to the Date object. Parameters: event (str): The event to add. """ self.events.add(event) def __str__(self): """Return string representation of Date object.""" sorted_events = sorted(self.events) return "{}: {}".format(self.date, ", ".join(sorted_events)) class DateSet: """Class to represent a collection of dates.""" def __init__(self): """Initialize DateSet object.""" self.dates = {} def add_date(self, date): """Add a date to the DateSet. Parameters: date (Date): The Date object to add. """ self.dates[date.date] = date def __str__(self): """Return string representation of DateSet object.""" sorted_dates = sorted(self.dates.values(), key=lambda x: x.date) result = [] for date in sorted_dates: result.append(str(date)) return "\n".join(result)
def canonicalize_date(date_str): """Convert date string to canonical format. Parameters: date_str (str): The date string to canonicalize. Returns: str: The date in canonical format. """ parts = date_str.split() if len(parts) == 3: # Parsing date with MonthName dd yyyy format month_abbr = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4,\ 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8,\ 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} month = month_abbr.get(parts[0][:3], None) if month is None: print("Error: Invalid month abbreviation") return None day = int(parts[1]) year = int(parts[2]) elif "/" in date_str: # Parsing date with mm/dd/yyyy format parts = date_str.split("/") month = int(parts[0]) day = int(parts[1]) year = int(parts[2]) else: # Parsing date with yyyy-mm-dd format parts = date_str.split("-") year = int(parts[0]) month = int(parts[1]) day = int(parts[2]) return "{:d}-{:d}-{:d}".format(year, month, day) def main(): """Main function to read input file and process operations.""" input_file = input() date_set = DateSet() file = open(input_file, 'r') for line in file: operation, rest = line.strip().split(' ', 1) if operation == 'I': # Insert operation date_str, event = rest.split(':', 1) date_canonical = canonicalize_date(date_str.strip()) if date_canonical not in date_set.dates: date = Date(date_canonical, event.strip()) date_set.add_date(date) else: date_set.dates[date_canonical].add_event(event.strip()) elif operation == 'R': # Retrieve operation date_str = rest.strip() date_canonical = canonicalize_date(date_str) if date_canonical in date_set.dates: for event in sorted(date_set.dates[date_canonical].events): print("{}: {}".format(date_canonical, event))
else: # Invalid operation print("Error - Illegal operation.") file.close() main()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help