Skip to content

Commit 3c8f221

Browse files
committed
added comments and fixed display not showing hours
1 parent ff1676a commit 3c8f221

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

planner.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import sqlite3 as sql
22
import time
33

4+
# create DB
45
connect = sql.connect('planner.db')
56
cursor = connect.cursor()
67

8+
# create tables in DB
79
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT UNIQUE, time REAL, year TEXT, month TEXT, day TEXT, type_id INTEGER, FOREIGN KEY(type_id) REFERENCES types(type_id))")
810
cursor.execute("CREATE TABLE IF NOT EXISTS types (type_id INTEGER PRIMARY KEY, type TEXT UNIQUE)")
911

12+
# insert types into the types table
1013
cursor.execute('SELECT * FROM types')
1114
if len(cursor.fetchall()) == 0:
1215
count = 0
@@ -26,6 +29,7 @@
2629
connect.commit()
2730

2831
def get_choice(max, phrase, do_phrase=True):
32+
# gets the user's choice for the given amount of numbers
2933
choice = 0
3034
while choice > max or choice < 1:
3135
try:
@@ -42,22 +46,27 @@ def get_choice(max, phrase, do_phrase=True):
4246
return choice
4347

4448
def get_all():
49+
# returns all of the data needed to show all tasks and their types, due dates, and times
4550
cursor.execute("SELECT ta.task, ty.type, (ta.month || '/' || ta.day || '/' || ta.year) AS date, ta.time FROM tasks ta JOIN types ty ON ta.type_id = ty.type_id ORDER BY ta.year, ta.month, ta.day, ta.time")
4651
return cursor.fetchall()
4752

4853
def get_tasks():
54+
# returns only the task names
4955
cursor.execute("SELECT task FROM tasks")
5056
return cursor.fetchall()
5157

5258
def get_types():
59+
# returns only the type names
5360
cursor.execute("SELECT * FROM types")
5461
return cursor.fetchall()
5562

5663
def get_value(data, new=False):
64+
# get's the value for the given variable (time or date)
5765
value = -1
5866
while value < 0:
5967
try:
6068
if data == 'hours':
69+
# get the amount of hours
6170
value = float(input('\nTime to complete in hours: '))
6271
elif data == 'type':
6372
while value > 5 or value < 1:
@@ -77,6 +86,7 @@ def get_value(data, new=False):
7786
for j in i:
7887
now_year = j
7988
while not correct:
89+
# get the year the task is due
8090
year = input('\nDue date year (yyyy): ')
8191
if len(year) != 4 or int(year) < 0 or year < now_year:
8292
print('\nNot a valid number.')
@@ -91,7 +101,8 @@ def get_value(data, new=False):
91101
current_date = cursor.fetchall()
92102
for i in current_date:
93103
for j in i:
94-
now_month = j
104+
now_month = j
105+
# get the month the task is due
95106
month = input('\nDue date month (mm): ')
96107
try:
97108
int(month)
@@ -111,11 +122,13 @@ def get_value(data, new=False):
111122
for j in i:
112123
now_day = j
113124
while not correct:
125+
# get the day the task is due
114126
day = input('\nDue date day (dd): ')
115127
try:
116128
int(day)
117129
except:
118130
caught = True
131+
# error checking and test to see if leap year for correct amount of days if month is february
119132
if caught or (int(day) < 1) or len(day) != 2 or (int(month) in {1, 3, 5, 7, 8, 10, 12} \
120133
and int(day) > 31) or (int(month) in {4, 6, 9, 11} and int(day) > 30) or (month == \
121134
'02' and (int(year) % 400 == 0 or int(year) % 4 == 0 and int(year) % 100 != 0) \
@@ -127,6 +140,7 @@ def get_value(data, new=False):
127140
else:
128141
correct = True
129142
date.append(day)
143+
# if trying to get date value, return date list of year, month, day
130144
return date
131145
if value < 0:
132146
print('\nNot a valid number.')
@@ -135,36 +149,44 @@ def get_value(data, new=False):
135149
print('\nNot a valid number.')
136150
time.sleep(.5)
137151
value = -1
152+
# if trying to get time value, return time value
138153
return value
139154

140155
def display_tasks():
156+
# displays the stored tasks created by the user from the DB
141157
tasks = get_all()
142158
print('\n{:<20} {:<20} {:<20} {:<20}'.format('Task', 'Type', 'Due', 'Time'))
143159
print('{:<20} {:<20} {:<20} {:<20}'.format('-----', '-----', '----', '-----'))
144160
for task in tasks:
145-
print('{:<20} {:<20} {:<20} {:<1}'.format(task[0], task[1], task[2], task[3], 'hours'))
161+
print('{:<20} {:<20} {:<20} {:<1} {:<1}'.format(task[0], task[1], task[2], task[3], 'hours'))
146162

147163
def display_types():
164+
# displays all the types of tasks
148165
types = get_types()
149166
print('\n{:<15} {:<15}'.format('Type ID', 'Type'))
150167
print('{:<15} {:<15}'.format('--------', '-----'))
151168
for type in types:
152169
print('{:<15} {:<15}'.format(type[0], type[1]))
153170

171+
# begin main
154172
print('Welcome to your planner!')
155173

156174
choice = None
157175
while choice != 3:
176+
# beginning user input choice
158177
choice = get_choice(3, '\nWhat would you like to do?\n1). View Tasks\n2). Edit Planner\n3). Quit')
159178

160179
if choice == 1:
180+
# to view the tasks
161181
display_tasks()
162182

163183

164184
elif choice == 2:
185+
# gives them the choice of what they want to edit
165186
choice = get_choice(5, '\nWould you like to:\n1). Add\n2). Edit\n3). Delete\n4). Reset planner\n5). Go back')
166187

167188
if choice == 1:
189+
# if user wants to add a task, get task, type, date, and time and insert into the DB
168190
passed = False
169191
while not passed:
170192
bad = False
@@ -190,6 +212,7 @@ def display_types():
190212
connect.commit()
191213

192214
elif choice == 2:
215+
# if user wants to edit a task, ask which task
193216
display_tasks()
194217
tasks = get_tasks()
195218
bad = True
@@ -206,28 +229,34 @@ def display_types():
206229
if bad:
207230
print('\nNot a valid task.')
208231
time.sleep(.5)
232+
# ask what they want to change from the task they chose
209233
choice = get_choice(4, '\nWould you like to edit:\n1). Task\n2). Type\n3). Due date\n4). Time')
210234
if choice == 1:
235+
# update the task name
211236
task = input('Task: ')
212237
values = (task, edit)
213238
cursor.execute("UPDATE tasks SET task = ? WHERE task = ?", values)
214239
elif choice == 2:
240+
# update the type
215241
display_types()
216242
type_id = get_value('type', True)
217243
values = (type_id, edit)
218244
cursor.execute("UPDATE tasks SET type_id = ? WHERE task = ?", values)
219245
elif choice == 3:
246+
# update the due date
220247
choice = None
221248
date = get_value('date')
222249
values = (date[0], date[1], date[2], edit)
223250
cursor.execute("UPDATE tasks SET year = ?, month = ?, day = ? WHERE task = ?", values)
224251
elif choice == 4:
252+
# update the time
225253
hours = get_value('hours')
226254
values = (hours, edit)
227255
cursor.execute("UPDATE tasks SET time = ? WHERE task = ?", values)
228256
connect.commit()
229257

230258
elif choice == 3:
259+
# if user wants to delete a task, ask which task
231260
choice = 0
232261
display_tasks()
233262
print('\nWhich task would you like to delete?')
@@ -237,6 +266,7 @@ def display_types():
237266
connect.commit()
238267

239268
elif choice == 4:
269+
# if user wants to reset the planner entirely, make sure and then delete everything from the DB
240270
verify = input('\nAre you sure you want to reset the planner (y/n)? ').lower()
241271
if verify == 'y':
242272
cursor.execute('DELETE FROM tasks')

0 commit comments

Comments
 (0)