Skip to content

Commit

Permalink
Merge pull request #8 from sajarin/shaedil
Browse files Browse the repository at this point in the history
Improve statistical logs and app drawing over apps
  • Loading branch information
sajarin authored Mar 29, 2022
2 parents 3a209f0 + 8c32a4b commit ec8818e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.txt
statistics.txt
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# pomodoro
# 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
```
74 changes: 71 additions & 3 deletions pomodoro.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
Options:
-h, --help Show this help message and exit
--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():
Expand All @@ -35,7 +36,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:
Expand All @@ -48,10 +54,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:
Expand All @@ -62,7 +86,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:
Expand All @@ -88,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
Expand All @@ -105,6 +128,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
Expand All @@ -126,6 +193,7 @@ def reward_alert() -> None:
popup if reward
"""
root = tk.Tk()
root.attributes('-topmost')
root.withdraw()
messagebox.showwarning(
'You got a Reward!',
Expand Down
Empty file added statistics.txt
Empty file.

0 comments on commit ec8818e

Please sign in to comment.