-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.py
116 lines (100 loc) · 3.54 KB
/
counter.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
from datetime import datetime
class Counter:
"""
A class to represent a habit with a counter for tracking increments over time.
Attributes:
----------
id : int
Unique identifier for the habit.
name : str
Name of the habit.
description : str
Description of the habit.
periodicity : str
Frequency of the habit (e.g., "Daily", "Weekly").
creation_date : str
The date and time when the habit was created.
Methods:
-------
store(db):
Stores the habit in the database.
increment(db, increment_date=None):
Increments the counter for the habit, optionally using a specified date.
reset(db):
Resets the counter for the habit.
delete(db):
Deletes the habit and its counters from the database.
"""
def __init__(self, name, description, periodicity, id=None):
"""
Constructs all the necessary attributes for the Counter object.
Parameters:
----------
name : str
Name of the habit.
description : str
Description of the habit.
periodicity : str
Frequency of the habit (e.g., "Daily", "Weekly").
id : int, optional
Unique identifier for the habit (default is None).
"""
self.id = id
self.name = name
self.description = description
self.periodicity = periodicity
self.creation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def store(self, db):
"""
Stores the habit in the database.
Parameters:
----------
db : sqlite3.Connection
The database connection.
"""
cursor = db.cursor()
cursor.execute('''INSERT INTO habits (name, description, periodicity, creation_date)
VALUES (?, ?, ?, ?)''', (self.name, self.description, self.periodicity, self.creation_date))
db.commit()
self.id = cursor.lastrowid
def increment(self, db, increment_date=None):
"""
Increments the counter for the habit, optionally using a specified date.
Parameters:
----------
db : sqlite3.Connection
The database connection.
increment_date : datetime, optional
The date to use for the increment (default is None, which uses the current date and time).
"""
cursor = db.cursor()
if increment_date is None:
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
else:
current_time = increment_date.strftime("%Y-%m-%d %H:%M:%S")
cursor.execute('''INSERT INTO counters (habit_id, increment_date)
VALUES (?, ?)''', (self.id, current_time))
db.commit()
def reset(self, db):
"""
Resets the counter for the habit by deleting all related entries in the counters table.
Parameters:
----------
db : sqlite3.Connection
The database connection.
"""
cursor = db.cursor()
cursor.execute('''DELETE FROM counters WHERE habit_id = ?''', (self.id,))
db.commit()
def delete(self, db):
"""
Deletes the habit and its counters from the database.
Parameters:
----------
db : sqlite3.Connection
The database connection.
"""
cursor = db.cursor()
cursor.execute('''DELETE FROM habits WHERE id = ?''', (self.id,))
cursor.execute('''DELETE FROM counters WHERE habit_id = ?''', (self.id,))
db.commit()