I made this to record the time I spend practicing Python. I'll eventually add in functionality to visualize the data. I'd appreciate any feedback that will help me improve.
# timer.py - A timer to track time spent practicing python
from datetime import timedelta
from datetime import datetime as Datetime
# used json b/c I wanted to practice it
import json
import os
# dict with all python practice options
options = ["Code Review",
"Project",
"Puzzles (Project Euler, Finxter, kaggle)",
"Courses, Books, or Videos",
"Read Docs"]
# Prompts user what type of practice they would like to do. User enters int.
def practice_choice():
print("Pick a form of practice:\n")
# print each option in options then ask user to select
num = 1
for option in options:
print(str(num) + ". " + option)
num += 1
selection = input("\nPick a number: ")
return options[int(selection) - 1]
def timer(selection):
# store notes about practice session
notes = input("Enter notes... ")
# start timing session and get start time
input("Hit Enter to begin practice...")
start_dt = Datetime.now()
# loop to allow for pausing. infinite loop that breaks when user clicks enter to stop session.
var = 1
total_so_far = 0
while var == 1:
pause_or_stop = input("Hit Enter to stop or space to pause...")
# when user hits enters space char
if pause_or_stop == ' ':
# gets time session was paused. Calculates time practicing so far. Stores for later
# Allows user to pause multiple times
pause_dt = Datetime.now()
total_so_far += int((start_dt - pause_dt) / timedelta(seconds=1)) # used sec for easier testing.
# resume on enter key and overwrites old start time
input("Hit Enter to resume")
start_dt = Datetime.now()
# breaks if enter key
elif pause_or_stop == '':
break
# catch bad input
else:
print("Key not recognized")
# get stop time and find total time in minutes
end_dt = Datetime.now()
total_mins = int((end_dt - start_dt) / timedelta(seconds=1)) + total_so_far # used sec for easier testing.
# add value to data[selection] dict. value is a list of datetime info, noes, total_minutes
data[selection].append({'day': Datetime.now().strftime("%A"), # %b %d %Y %H:%M),
'date': Datetime.now().strftime("%m-%d-%y"),
'time': Datetime.now().strftime("%H:%M"),
'notes': notes,
'minutes': total_mins})
# convert dict to JSON object
with open(target_file, 'w') as json_file:
json.dump(data, json_file, indent=4)
# prompt user for type of practice
choice = practice_choice()
# practice_log file with all practice sessions are stored
target_file = "practice_log.json"
# initialize data dict which will hold practice logs
data = {}
# creates json file if it doesn't exist
if not os.path.exists(target_file):
# top level values of json are created corresponding to practice type
for each_item in options:
data[each_item] = []
with open(target_file, 'w') as json_file:
json.dump(data, json_file, indent=4)
# open json file and load into target_files
with open(target_file, 'r') as json_file:
data = json.load(json_file)
# starts timer
timer(choice)