Spaces:
Runtime error
Runtime error
# Goal here is to create a tool that will create and maintain a planning | |
# all activities and timestamp will be stored as a json file | |
# The tool will have the following methods: | |
# - add_activity(activity:str, timestamp:str) -> str | |
# - get_activities() -> str | |
# - get_activities_by_date(date:str) -> str | |
# - get_activities_by_month(month:str) -> str | |
# - get_activities_by_year(year:str) -> str | |
# The tool will have the following attributes: | |
# - activities: a dictionary that will store the activities and timestamps | |
# - filename: a string that will store the name of the json file | |
####----------------------------------------------------------------------------------------------------#### | |
import json | |
from datetime import datetime | |
class PlanningTool: | |
def __init__(self, filename:str): | |
self.filename = filename | |
self.activities = {} | |
try: | |
with open(self.filename, 'r') as file: | |
self.activities = json.load(file) | |
except: | |
with open(self.filename, 'w') as file: | |
json.dump(self.activities, file) | |
def add_activity(self, activity:str, timestamp:str) -> str: | |
self.activities[activity] = timestamp | |
with open(self.filename, 'w') as file: | |
json.dump(self.activities, file) | |
return f"Activity '{activity}' added successfully." | |
def get_activities(self) -> str: | |
return json.dumps(self.activities, indent=4) | |
def get_activities_by_date(self, date:str) -> str: | |
activities_by_date = {activity:timestamp for activity, timestamp in self.activities.items() if timestamp.split(' ')[0] == date} | |
return json.dumps(activities_by_date, indent=4) | |
def get_activities_by_month(self, month:str) -> str: | |
activities_by_month = {activity:timestamp for activity, timestamp in self.activities.items() if timestamp.split('-')[1] == month} | |
return json.dumps(activities_by_month, indent=4) | |
def get_activities_by_year(self, year:str) -> str: | |
activities_by_year = {activity:timestamp for activity, timestamp in self.activities.items() if timestamp.split('-')[0] == year} | |
return json.dumps(activities_by_year, indent=4) | |