File size: 2,205 Bytes
bebf791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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)