/ project.py
project.py
1 import csv 2 import os 3 import datetime 4 import tabulate 5 import sys 6 7 FIELDS = ['Habit', 'Days', 'Date', 'Logs'] 8 DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] 9 NAME = 'habits.csv' 10 11 def main(): 12 clear() 13 14 print('Welcome to Habit Tracker!') 15 print('What would you like to do?') 16 print('1. Create a new habit') 17 print('2. Log a habit') 18 print('3. View habits') 19 20 choice = input('Enter the number of your choice: ') 21 match choice: 22 case '1': 23 clear() 24 create(NAME) 25 if input('press 1 to go back to the main menu or anything else to quit.') == '1': 26 main() 27 else: 28 sys.exit() 29 case '2': 30 clear() 31 log(NAME) 32 if input('press 1 to go back to the main menu or anything else to quit.') == '1': 33 main() 34 else: 35 sys.exit() 36 case '3': 37 clear() 38 print('Here are your habits:') 39 print(view(NAME)) 40 coy = input('press 1 to go back to the main menu or anything else to quit.') 41 match coy: 42 case '1': 43 main() 44 case _: 45 sys.exit() 46 47 case _: 48 sys.exit() 49 50 51 def create(file=NAME): 52 with open(file, 'a+', newline='') as f: 53 54 writer = csv.writer(f) 55 hname = input('Enter the name of the habit: ') 56 freq = input('Enter the number of days in a week you want to repeat this habit: ') 57 hdays = [] 58 hdate = datetime.date.today() 59 hlogs = 0 60 61 for _ in range(int(freq)): 62 day = input('Enter the day: ') 63 if day not in DAYS: 64 print('Invalid day. Please try again.') 65 sys.exit() 66 else: 67 hdays.append(day) 68 69 writer.writerow([hname, hdays, hdate, hlogs]) 70 71 print('Habit created successfully!') 72 print(view(NAME)) 73 74 75 def log(file=NAME): 76 with open(file, 'r') as f: 77 reader = csv.reader(f) 78 todaylog = [] 79 lines = [] 80 81 for row in reader: 82 if tod(row[1], datetime.datetime.today()): 83 todaylog.append(row) 84 lines.append(row) 85 86 print('Here are the habits you need to log today:') 87 print(tabulate.tabulate(todaylog, tablefmt='fancy_grid', headers=FIELDS)) 88 89 hname = input('Enter the name of the habit you want to log: ') 90 flag = False 91 for n in range(len(lines)): 92 if lines[n][0] == hname: 93 lines[n][3] = 1 + int(lines[n][3]) 94 print('Habit logged successfully!') 95 flag = True 96 break 97 else: 98 continue 99 if not flag: 100 print('Habit not found. Please try again.') 101 sys.exit() 102 with open(file, 'w', newline='') as f: 103 writer = csv.writer(f) 104 writer.writerows(lines) 105 106 107 108 def view(file=NAME): 109 with open(file, 'r') as f: 110 reader = csv.reader(f) 111 return tabulate.tabulate(reader, headers=FIELDS, tablefmt='fancy_grid') 112 113 114 def clear(): 115 if os.name == 'nt': 116 os.system('cls') 117 else: 118 os.system('clear') 119 120 121 def tod(x, date) -> bool: 122 day = date.weekday() 123 today = DAYS[day] 124 if today in x: 125 return True 126 else: 127 return False 128 129 130 def createcsv(file): 131 with open(file, 'w', newline='') as f: 132 writer = csv.writer(f) 133 134 135 if not os.path.exists(NAME): 136 createcsv(NAME) 137 138 if __name__ == '__main__': 139 main()