From 793667291457380a0341f2ce7f3d25934de56b5b Mon Sep 17 00:00:00 2001 From: matin Date: Mon, 21 Apr 2025 18:06:36 +0200 Subject: first try to add commentary and save functionalities by Claude --- calendar_manager.py | 286 +++++++++++++++++++++++++++------------------------- 1 file changed, 151 insertions(+), 135 deletions(-) (limited to 'calendar_manager.py') diff --git a/calendar_manager.py b/calendar_manager.py index b62cf5d..be19b2f 100644 --- a/calendar_manager.py +++ b/calendar_manager.py @@ -1,136 +1,152 @@ -# calendar_manager.py -import json -import uuid -from datetime import datetime - -class CalendarEntry: - def __init__(self, start_date, end_date, keyword, entry_id=None, corrected_start_date=None, corrected_end_date=None): - self.id = entry_id if entry_id else str(uuid.uuid4()) - # Convert string dates to datetime if necessary - self.start_date = ( - datetime.fromisoformat(start_date) - if isinstance(start_date, str) - else start_date - ) - self.end_date = ( - datetime.fromisoformat(end_date) - if isinstance(end_date, str) - else end_date - ) - self.keyword = keyword - self.corrected_start_date = corrected_start_date - self.corrected_end_date = corrected_end_date - def to_dict(self): - return { - 'id': self.id, - 'start_date': self.start_date.isoformat(), - 'end_date': self.end_date.isoformat(), - 'keyword': self.keyword, - 'corrected_start_date': self.corrected_start_date.isoformat() if self.corrected_start_date else None, - 'corrected_end_date': self.corrected_end_date.isoformat() if self.corrected_end_date else None, - } - - @classmethod - def from_dict(cls, data): - return cls( - start_date=datetime.fromisoformat(data['start_date']), - end_date=datetime.fromisoformat(data['end_date']), - keyword=data['keyword'], - entry_id=data['id'], - corrected_start_date=datetime.fromisoformat(data['corrected_start_date']) if data.get('corrected_start_date') else None, - corrected_end_date=datetime.fromisoformat(data['corrected_end_date']) if data.get('corrected_end_date') else None, - ) - - def __repr__(self): - return (f"CalendarEntry(id={self.id}, start_date={self.start_date}, " - f"end_date={self.end_date}, keyword='{self.keyword}', " - f"corrected_start_date={self.corrected_start_date}, corrected_end_date={self.corrected_end_date})") - -class CalendarManager: - def __init__(self, filename=None): - self.filename = filename - self.entries = [] - if filename: - self.entries = self.add_entries_from_file(filename) - - def switch_file(self, new_filename): - """Switch to a new file.""" - self.filename = new_filename - - def load_file(self, new_filename): - """Clears current entries and loads entries from new file.""" - self.clear_entries() - self.add_entries_from_file(new_filename) - self.switch_file(new_filename) - - def add_entries_from_file(self, file_path): - """Add events from another JSON file to the current calendar.""" - try: - with open(file_path, 'r') as f: - data = json.load(f) - new_entries = [CalendarEntry.from_dict(entry) for entry in data] - self.entries.extend(new_entries) - self.save_entries() - return new_entries - except (FileNotFoundError, json.JSONDecodeError) as e: - print(f"Error reading file {file_path}: {e}") - return [] - - def clear_entries(self): - self.entries = [] - - def save_entries(self): - """Save the current list of events to the file.""" - if self.filename: - try: - with open(self.filename, 'w') as f: - json.dump([entry.to_dict() for entry in self.entries], f, indent=4) - except Exception as e: - print(f"Error writing to file {self.filename}: {e}") - - def add_entry(self, start_date, end_date, keyword): - """Add a new event to the calendar.""" - new_entry = CalendarEntry(start_date, end_date, keyword) - self.entries.append(new_entry) - self.save_entries() - return new_entry - - def modify_entry(self, entry_id, start_date=None, end_date=None, keyword=None): - """Modify an existing event by ID.""" - entry = self.get_entry_by_id(entry_id) - if entry: - if start_date: - entry.start_date = start_date - if end_date: - entry.end_date = end_date - if keyword: - entry.keyword = keyword - self.save_entries() - return entry - return None - - def delete_entry(self, entry_id): - """Delete an event by ID.""" - entry = self.get_entry_by_id(entry_id) - if entry: - self.entries.remove(entry) - self.save_entries() - return entry - return None - - def get_entry_by_id(self, entry_id): - """Get an event by its ID.""" - return next((entry for entry in self.entries if entry.id == entry_id), None) - - def list_entries(self): - """List all calendar entries.""" - return self.entries - - def correct_dates(self, list_of_events): - for start_date, end_date, original_id in list_of_events: - entry = self.get_entry_by_id(original_id) - entry.corrected_start_date = start_date - entry.corrected_end_date = end_date - - +# calendar_manager.py +import json +import uuid +from datetime import datetime + +class CalendarEntry: + def __init__(self, start_date, end_date, keyword, entry_id=None, corrected_start_date=None, corrected_end_date=None, commentary=None): + self.id = entry_id if entry_id else str(uuid.uuid4()) + # Convert string dates to datetime if necessary + self.start_date = ( + datetime.fromisoformat(start_date) + if isinstance(start_date, str) + else start_date + ) + self.end_date = ( + datetime.fromisoformat(end_date) + if isinstance(end_date, str) + else end_date + ) + self.keyword = keyword + self.corrected_start_date = corrected_start_date + self.corrected_end_date = corrected_end_date + self.commentary = commentary + + def to_dict(self): + return { + 'id': self.id, + 'start_date': self.start_date.isoformat(), + 'end_date': self.end_date.isoformat(), + 'keyword': self.keyword, + 'corrected_start_date': self.corrected_start_date.isoformat() if self.corrected_start_date else None, + 'corrected_end_date': self.corrected_end_date.isoformat() if self.corrected_end_date else None, + 'commentary': self.commentary, + } + + @classmethod + def from_dict(cls, data): + return cls( + start_date=datetime.fromisoformat(data['start_date']), + end_date=datetime.fromisoformat(data['end_date']), + keyword=data['keyword'], + entry_id=data['id'], + corrected_start_date=datetime.fromisoformat(data['corrected_start_date']) if data.get('corrected_start_date') else None, + corrected_end_date=datetime.fromisoformat(data['corrected_end_date']) if data.get('corrected_end_date') else None, + commentary=data.get('commentary'), + ) + + def __repr__(self): + return (f"CalendarEntry(id={self.id}, start_date={self.start_date}, " + f"end_date={self.end_date}, keyword='{self.keyword}', " + f"corrected_start_date={self.corrected_start_date}, corrected_end_date={self.corrected_end_date}, " + f"commentary={repr(self.commentary)})") + +class CalendarManager: + def __init__(self, filename=None): + self.filename = filename + self.entries = [] + if filename: + self.entries = self.add_entries_from_file(filename) + + def switch_file(self, new_filename): + """Switch to a new file.""" + self.filename = new_filename + + def load_file(self, new_filename): + """Clears current entries and loads entries from new file.""" + self.clear_entries() + self.add_entries_from_file(new_filename) + self.switch_file(new_filename) + + def add_entries_from_file(self, file_path): + """Add events from another JSON file to the current calendar.""" + try: + with open(file_path, 'r') as f: + data = json.load(f) + new_entries = [CalendarEntry.from_dict(entry) for entry in data] + self.entries.extend(new_entries) + self.save_entries() + return new_entries + except (FileNotFoundError, json.JSONDecodeError) as e: + print(f"Error reading file {file_path}: {e}") + return [] + + def clear_entries(self): + self.entries = [] + + def save_entries(self): + """Save the current list of events to the file.""" + if self.filename: + try: + with open(self.filename, 'w') as f: + json.dump([entry.to_dict() for entry in self.entries], f, indent=4) + except Exception as e: + print(f"Error writing to file {self.filename}: {e}") + + def add_entry(self, start_date, end_date, keyword, commentary=None): + """Add a new event to the calendar.""" + new_entry = CalendarEntry(start_date, end_date, keyword, commentary=commentary) + self.entries.append(new_entry) + self.save_entries() + return new_entry + + def modify_entry(self, entry_id, start_date=None, end_date=None, keyword=None, commentary=None): + """Modify an existing event by ID.""" + entry = self.get_entry_by_id(entry_id) + if entry: + if start_date: + entry.start_date = start_date + if end_date: + entry.end_date = end_date + if keyword: + entry.keyword = keyword + if commentary is not None: # Allow setting empty string as commentary + entry.commentary = commentary + self.save_entries() + return entry + return None + + def add_commentary(self, entry_id, commentary): + """Add or update commentary for an existing entry.""" + entry = self.get_entry_by_id(entry_id) + if entry: + entry.commentary = commentary + self.save_entries() + return entry + return None + + def delete_entry(self, entry_id): + """Delete an event by ID.""" + entry = self.get_entry_by_id(entry_id) + if entry: + self.entries.remove(entry) + self.save_entries() + return entry + return None + + def get_entry_by_id(self, entry_id): + """Get an event by its ID.""" + return next((entry for entry in self.entries if entry.id == entry_id), None) + + def list_entries(self): + """List all calendar entries.""" + return self.entries + + def correct_dates(self, list_of_events): + for start_date, end_date, original_id in list_of_events: + entry = self.get_entry_by_id(original_id) + entry.corrected_start_date = start_date + entry.corrected_end_date = end_date + + \ No newline at end of file -- cgit v1.1