-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhabitTracker.py
141 lines (112 loc) · 4.05 KB
/
habitTracker.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pickle
from typing import Optional, List
from recurrence import RecurrenceType
from habit import Habit
# Class for managing habits and habit-related actions
class HabitTracker:
_default_habit_file_path: str = "habits.pickle"
def __init__(self, habits: Optional[List[Habit]] =None):
if habits is None:
habits = []
self._habits = habits
@staticmethod
def default_habit_file_path() -> str:
"""
Get the default habit file path.
:return: The default habit file path.
"""
return HabitTracker._default_habit_file_path
@property
def habits(self) -> List[Habit]:
"""
Get the list of habits.
:return: A list of habit strings.
"""
return self._habits
@habits.setter
def habits(self, value: List[Habit]):
"""
Set the list of habits.
:param value: A list of new habits.
"""
self._habits = value
def add_habit(self, habit: Habit):
"""
Adds a habit to the habit tracker.
Args:
habit (Habit): The habit to be added.
"""
self._habits.append(habit)
def get_number_of_habits(self) -> int:
"""
Returns the number of habits in the tracker.
Returns:
int: The number of habits.
"""
return len(self._habits)
def list_habits_by_recurrence(self, recurrence_type: RecurrenceType):
"""
Lists habits with a specific recurrence type.
Args:
recurrence_type (RecurrenceType): The recurrence type to filter habits.
Returns:
list: List of habits with the specified recurrence type.
"""
return [habit for habit in self._habits if habit.recurrence and habit.recurrence.recurrence_type == recurrence_type]
def get_longest_streak_all_habits(self):
"""
Calculates the longest streak across all habits.
Returns:
int: The longest streak across all habits.
"""
if not self._habits:
return 0
longest_streak = 0
for habit in self._habits:
habit_streak = habit.get_longest_streak()
longest_streak = max(longest_streak, habit_streak)
return longest_streak
def get_longest_streak_for_habit(self, habit_index: int) -> Optional[int]:
"""
Calculates the longest streak for a specific habit.
Args:
habit_index (int): index of habit in the habits array of tracker.
Returns:
Optional[int]: The longest streak for the chosen habit, or None if the habit is not found.
"""
if 0 <= habit_index < len(self._habits):
habit: Optional[Habit] = self._habits[habit_index]
if habit:
habit_longest_streak = habit.get_longest_streak()
return habit_longest_streak
else:
return None
else:
return None
def save_habits_to_file(self, filename: str = default_habit_file_path, habits: Optional[List[Habit]] = None):
"""
Saves the habits to a file.
Args:
filename (str): The name of the file and path to save the habits.
habits (list of Habit): The list of habits to be saved.
"""
if not habits:
habits = self._habits
try:
with open(filename, 'wb') as file:
pickle.dump(habits, file)
print(f'Habits saved to {filename}')
except Exception as e:
print(f'Error saving habits to {filename}: {e}')
def load_habits_from_file(self, filename: str = default_habit_file_path):
"""
Loads habits from a file.
Args:
filename (str): The name of the file pathto load habits from.
"""
try:
with open(filename, 'rb') as file:
self._habits = pickle.load(file)
print(f'Habits loaded from {filename}')
except Exception as e:
print(f'Error loading habits from {filename}: {e}')