This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhabit_manager.py
70 lines (59 loc) · 2.08 KB
/
habit_manager.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# habit_manager.py
from habit import Habit
import storage_module
class HabitManager:
"""
Manages habit operations.
"""
def __init__(self):
self.habits = {}
self.load_habits()
def load_habits(self):
"""Loads habits from the storage."""
habits_data = storage_module.load_habits()
for data in habits_data:
habit = Habit(
name=data['name'],
periodicity=data['periodicity'],
creation_date=data['creation_date'],
completion_dates=data['completion_dates']
)
self.habits[habit.name] = habit
def create_habit(self, name, periodicity):
"""Creates a new habit."""
if name in self.habits:
print(f"Habit '{name}' already exists.")
return
habit = Habit(name, periodicity)
self.habits[name] = habit
storage_module.save_habit(habit)
print(f"Habit '{name}' created.")
def delete_habit(self, name):
"""Deletes an existing habit."""
if name in self.habits:
del self.habits[name]
storage_module.delete_habit(name)
print(f"Habit '{name}' deleted.")
else:
print(f"Habit '{name}' does not exist.")
def get_habit(self, name):
"""Retrieves a habit by name."""
return self.habits.get(name)
def list_habits(self, periodicity=None):
"""Lists all habits, optionally filtered by periodicity."""
if periodicity:
return [habit for habit in self.habits.values() if habit.periodicity == periodicity]
else:
return list(self.habits.values())
def complete_habit(self, name):
"""Marks a habit as completed."""
habit = self.get_habit(name)
if habit:
habit.complete()
storage_module.save_completion(habit)
print(f"Habit '{name}' marked as completed.")
else:
print(f"Habit '{name}' does not exist.")
def delete_all_habits(self):
"""Deletes all habits."""
self.habits.clear()