From ca5b2abb5ae554c62694db0ad4727a8be93c4fdc Mon Sep 17 00:00:00 2001 From: Shaedil Dider Date: Mon, 28 Mar 2022 22:34:56 -0400 Subject: [PATCH 1/4] feat: track the number of tasks done per day A file called statistics.txt logs the number of tasks done per day. The script compares the most recent date logged and today's date and checks if it needs to add a new entry (see below) or update the tasks done during the day. Entry format: DATETIME, MM/DD/YY NUM_OF_TASKS_DONE, # --- pomodoro.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- statistics.txt | 0 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 statistics.txt diff --git a/pomodoro.py b/pomodoro.py index 211880c..273776b 100644 --- a/pomodoro.py +++ b/pomodoro.py @@ -19,11 +19,13 @@ --version Show version and exit """ +import os import time import random import argparse import tkinter as tk from tkinter import messagebox +from datetime import date def main(): @@ -35,7 +37,12 @@ def main(): 'length_of_tasks_in_minutes': 0, 'num_of_tasks_done': 0 } + stats_contents = { + 'datetime': "", + 'num_of_tasks_done': 0 + } config = "config.txt" + stats = "statistics.txt" with open(config, "r") as handle: lines = [] for line in handle: @@ -48,10 +55,28 @@ def main(): task_length_in_seconds = int( 60*config_contents['length_of_tasks_in_minutes']) + with open(stats, "r") as handle: + lines = [] + raw_lines = handle.readlines()[-2:] + for line in raw_lines: + lines.append(line.split(",")[1].strip()) + stats_contents['datetime'] = str(lines[0]) + print(stats_contents['datetime']) + if is_today(stats_contents['datetime']): + stats_contents['num_of_tasks_done'] = int(lines[1]) + else: + stats_contents['num_of_tasks_done'] = 0 + print(stats_contents['num_of_tasks_done']) + while True: run_timer(task_length_in_seconds) config_contents['num_of_tasks_done'] += 1 + stats_contents['num_of_tasks_done'] += 1 update_config(config_contents, config) + update_statistics(stats_contents, stats, config_contents[ + 'num_of_tasks_done']) + # set new date to today's to avoid duplication of log entries + stats_contents['datetime'] = date.today().strftime("%m/%d/%y") reward_prob = get_reward_probability(config_contents) is_reward = check_reward(reward_prob) if is_reward: @@ -62,7 +87,7 @@ def handle_args() -> None: # handle script arguments parser = argparse.ArgumentParser(description='A timer + rng for skinnerian\ reinforcement') - args = parser.parse_args() + parser.parse_args() def run_timer(seconds: int) -> None: @@ -105,6 +130,50 @@ def update_config(contents: dict, config: str) -> None: handle.write(line) +def update_statistics(contents: dict, statistics: str, cur_tasks: int) -> None: + """ + Writes to the statistics file the current number of tasks done and date + """ + keys = list(contents.keys()) + tasks_line = keys[1].upper() + ", " + str(contents['num_of_tasks_done'] + ).upper() + '\n' + date_line = keys[0].upper() + ", " + date.today().strftime( + "%m/%d/%y") + '\n' + + # if date logged is today's, then edit 'num_of_tasks_done' line only + if is_today(contents['datetime']): + truncate_last_line_in_file(statistics) + with open(statistics, 'a', encoding='utf8') as handle: + handle.write('\n' + tasks_line) + # new date, new entry in log file and first task of the day + else: + tasks_line = keys[1].upper() + ", 1" + '\n' + with open(statistics, "a") as handle: + handle.write(date_line) + handle.write(tasks_line) + + +def is_today(saved_datetime: str) -> bool: + """Checks if today is the same date as the one logged""" + if saved_datetime == date.today().strftime("%m/%d/%y"): + return True + return False + + +def truncate_last_line_in_file(filename: str) -> None: + """Finds and removes the last line in a file""" + + with open(filename, "rb+") as handle: + handle.seek(0, os.SEEK_END) + pos = handle.tell() - 1 + while pos > 0 and handle.read(1) != b"\n": + pos -= 1 + handle.seek(pos, os.SEEK_SET) + if pos > 0: + handle.seek(pos - 1, os.SEEK_SET) + handle.truncate() + + def get_reward_probability(config_contents: dict) -> float: """ returns the probability of a reward given number of tasks done diff --git a/statistics.txt b/statistics.txt new file mode 100644 index 0000000..e69de29 From 8f73295f3790c14c60606acfcd234fb3a945e207 Mon Sep 17 00:00:00 2001 From: Shaedil Dider Date: Mon, 28 Mar 2022 23:00:38 -0400 Subject: [PATCH 2/4] chore: add .gitignore for log files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3277e68 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.txt +statistics.txt From df2445842323fdb254df9f58445f14c96d0a9e64 Mon Sep 17 00:00:00 2001 From: Shaedil Dider Date: Mon, 28 Mar 2022 23:06:18 -0400 Subject: [PATCH 3/4] fix: make reward alert draw over other apps Fixes #4. --- pomodoro.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pomodoro.py b/pomodoro.py index 273776b..b4d03aa 100644 --- a/pomodoro.py +++ b/pomodoro.py @@ -16,7 +16,6 @@ Options: -h, --help Show this help message and exit - --version Show version and exit """ import os @@ -113,7 +112,6 @@ def check_reward(reward_prob: float) -> bool: """ returns true if rng < reward_prob """ - rng = random.random() if rng < reward_prob: return True @@ -195,6 +193,7 @@ def reward_alert() -> None: popup if reward """ root = tk.Tk() + root.attributes('-topmost') root.withdraw() messagebox.showwarning( 'You got a Reward!', From 8c32a4b7d95d1d5acbb0b5ca0ff96d5c5b6cedb2 Mon Sep 17 00:00:00 2001 From: Shaedil Dider Date: Mon, 28 Mar 2022 23:07:50 -0400 Subject: [PATCH 4/4] docs: update readme with config file instructions --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 76ae643..5f1a942 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# pomodoro \ No newline at end of file +# pomodoro + +```pomodoro.py``` requires a ```config.txt``` file to be instantiated before use. Here is a sample file: +``` +LENGTH_OF_TASKS_IN_MINUTES, 5.0 +NUM_OF_TASKS_DONE, 0 +```