From 5a03b3b1336be8edaf793b95946cc21bf479843e Mon Sep 17 00:00:00 2001 From: daviddoji Date: Tue, 5 Oct 2021 18:11:25 +0200 Subject: [PATCH] [WIP] Programmatically update README with results --- src/update_results_table.py | 67 +++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/src/update_results_table.py b/src/update_results_table.py index ef800e3..8c36dab 100644 --- a/src/update_results_table.py +++ b/src/update_results_table.py @@ -1,7 +1,14 @@ + +import os +import sys import time from glob import glob from pathlib import Path -from subprocess import PIPE, run +from subprocess import PIPE, STDOUT, run + +from pytablewriter import MarkdownTableWriter +from pytablewriter.style import Style + def read_md(): """ @@ -11,49 +18,43 @@ def read_md(): md = [line.strip() for line in f.readlines() if line.strip() != ""][1:] return md -def write_md(): +def write_md(results): """ Write to README.md """ - pass + with open("../dummy.md", "w") as f: + f.write(results) -def execute(num): +def execute(): """ Execute script and return output """ - # import sys - # sys.path.insert(0, "./Python/") + os.chdir(Path("Python/")) + scripts = sorted(glob("*[0-9].py")) - # from Python import Problem001 - - # res = eval(f"Problem{num:0>3}.compute()") - # print(res) - # time = str(res).split(sep=" ") - # print(time) - scripts = sorted(Path("Python/").glob("*[0-9].py")) - # print(scripts[0].name) + timings, results = [], [] for script in scripts: - run(["python3", f"{script}"]) - time.sleep(5) + _res = run(["python3", f"{script}"], capture_output=True, text=True).stdout.split("\n") + timings.append(_res[0]) + results.append(_res[2]) + writer = MarkdownTableWriter( + table_name="Project Euler Solutions", + headers=["Problem #", "Result", "Execution time (Python)"], + value_matrix=[[int(t[25:27]), int(r[23:]), t[29:]] for t,r in zip(timings, results)], + column_styles=[ + Style(align="center"), + Style(align="right", thousand_separator="_"), + Style(align="right"), + ], # specify styles for each column + margin=1 # add a whitespace for both sides of each cell + ) + writer.write_table() + return writer.dumps() if __name__ == "__main__": md = read_md() - execute(1) - - -# for c in chunks: -# res = run( -# [ -# "sbatch", -# "--parsable", -# "--dependency=afterok:" + ":".join(str(j) for j in images_nbs), -# "run2.sh", -# str(f"{c:02}"), -# your_path_for_notebooks.as_posix(), -# ], -# stdout=PIPE, -# check=True, -# cwd=Path("."), -# ) \ No newline at end of file + # print(md) + res = execute() + write_md(res)