Spaces:
Runtime error
Runtime error
Create organisation_tools.py
Browse files- organisation_tools.py +53 -0
organisation_tools.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Goal here is to create a tool that will create and maintain a planning
|
2 |
+
# all activities and timestamp will be stored as a json file
|
3 |
+
|
4 |
+
# The tool will have the following methods:
|
5 |
+
# - add_activity(activity:str, timestamp:str) -> str
|
6 |
+
# - get_activities() -> str
|
7 |
+
# - get_activities_by_date(date:str) -> str
|
8 |
+
# - get_activities_by_month(month:str) -> str
|
9 |
+
# - get_activities_by_year(year:str) -> str
|
10 |
+
|
11 |
+
# The tool will have the following attributes:
|
12 |
+
# - activities: a dictionary that will store the activities and timestamps
|
13 |
+
# - filename: a string that will store the name of the json file
|
14 |
+
|
15 |
+
####----------------------------------------------------------------------------------------------------####
|
16 |
+
|
17 |
+
import json
|
18 |
+
from datetime import datetime
|
19 |
+
|
20 |
+
class PlanningTool:
|
21 |
+
def __init__(self, filename:str):
|
22 |
+
self.filename = filename
|
23 |
+
self.activities = {}
|
24 |
+
try:
|
25 |
+
with open(self.filename, 'r') as file:
|
26 |
+
self.activities = json.load(file)
|
27 |
+
except:
|
28 |
+
with open(self.filename, 'w') as file:
|
29 |
+
json.dump(self.activities, file)
|
30 |
+
|
31 |
+
def add_activity(self, activity:str, timestamp:str) -> str:
|
32 |
+
self.activities[activity] = timestamp
|
33 |
+
with open(self.filename, 'w') as file:
|
34 |
+
json.dump(self.activities, file)
|
35 |
+
return f"Activity '{activity}' added successfully."
|
36 |
+
|
37 |
+
def get_activities(self) -> str:
|
38 |
+
return json.dumps(self.activities, indent=4)
|
39 |
+
|
40 |
+
def get_activities_by_date(self, date:str) -> str:
|
41 |
+
activities_by_date = {activity:timestamp for activity, timestamp in self.activities.items() if timestamp.split(' ')[0] == date}
|
42 |
+
return json.dumps(activities_by_date, indent=4)
|
43 |
+
|
44 |
+
def get_activities_by_month(self, month:str) -> str:
|
45 |
+
activities_by_month = {activity:timestamp for activity, timestamp in self.activities.items() if timestamp.split('-')[1] == month}
|
46 |
+
return json.dumps(activities_by_month, indent=4)
|
47 |
+
|
48 |
+
def get_activities_by_year(self, year:str) -> str:
|
49 |
+
activities_by_year = {activity:timestamp for activity, timestamp in self.activities.items() if timestamp.split('-')[0] == year}
|
50 |
+
return json.dumps(activities_by_year, indent=4)
|
51 |
+
|
52 |
+
|
53 |
+
|