Created
October 19, 2022 07:40
-
-
Save AnotherTwinkle/f49fa1aef9fcce99830bd424a56ce75a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, os, sys | |
import json | |
import winsound | |
VALID_RESULTS = ['noop','skipped', 'unsolved', 'AC', 'WA', 'TLE', 'RTE', 'MLE'] | |
class TimerExceeded(Exception): | |
pass | |
class Timer(object): | |
def __init__(self, start_time : int): | |
self.start_time = start_time | |
def compute_delta(self, cur = None): | |
cur = cur or int(time.time()) | |
delta = cur - self.start_time; | |
return delta | |
def format(self, s): | |
m = s // 60 | |
h = m // 60 | |
s %= 60 | |
m %= 60 | |
return f"{str(h).zfill(2)}:{str(m).zfill(2)}:{str(s).zfill(2)}" | |
def get(l, w): | |
return l[w] if w < len(l) else None | |
def dump_data(filename, problem, start_time, end_time, target_time, result, rating): | |
data = {}; | |
if not os.path.exists(filename): | |
with open(filename, 'w') as f: | |
json.dump({'data' : []}, f, indent = 2) | |
with open(filename, 'rb') as f: | |
data = json.load(f); | |
payload = { | |
'problem' : problem, | |
'start_time' : start_time, | |
'end_time' : end_time, | |
'target_time' : target_time, | |
'rating' : rating, | |
'result' : result | |
} | |
flag = 0; | |
for i in range(len(data['data'])): | |
if (data['data'][i]['problem'] == problem): | |
data['data'][i] = payload | |
flag = 1 | |
break | |
if not flag: | |
data['data'].append(payload) | |
with open(filename, 'w') as f: | |
json.dump(data, f, indent = 2) | |
def main(): | |
args = sys.argv | |
problem = get(args, 1) | |
target_time = get(args, 2) | |
rating = get(args, 3) | |
force = bool(get(args, 4)) | |
if problem == None or target_time == None: | |
print("Missing required arguments.") | |
return | |
target_time = int(target_time) * 60; | |
target_delta = Timer(0).format(target_time) | |
print('-' * 34) | |
print(f"Task : {problem}") | |
print(f"Rating : {rating or 'N/A'}") | |
print(f"Target delta : {target_delta}") | |
if force: | |
print("Force termination is ON.") | |
print("(Waiting for you to press Enter.)") | |
print('-' * 34) | |
print() | |
if input() != '': | |
print("Operation Terminated.") | |
return | |
start_time = int(time.time()) | |
timer = Timer(start_time) | |
try: | |
beeped = False | |
while (1): | |
time.sleep(1) | |
delta = timer.compute_delta() | |
if (delta >= target_time): | |
if not beeped: | |
winsound.MessageBeep() | |
beeped = True | |
if force: | |
raise TimerExceeded | |
print(timer.format(delta), end = '\r') | |
except TimerExceeded: | |
print("Timer exceeded.") | |
except KeyboardInterrupt: | |
print("User interrupted timer.") | |
end_time = int(time.time()) | |
taken_delta = timer.format(timer.compute_delta(end_time)) | |
print(f"{taken_delta} elapsed. (Target was {target_delta})") | |
while (1): | |
result = input('Solution Verdict : '); | |
if result in VALID_RESULTS: | |
break | |
else: | |
s = ' / '.join(VALID_RESULTS); | |
print(f"Please provide a valid verdict. ({s})") | |
if result == "noop": | |
print("Operation terminated.") | |
return | |
dump_data('data.json', problem, start_time, end_time, target_time, result, rating) | |
print("All OK.") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment