[WIP] Programmatically update README with results

This commit is contained in:
David Doblas Jiménez 2021-10-05 18:11:25 +02:00
parent 012480f7a3
commit 5a03b3b133

View File

@ -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("."),
# )
# print(md)
res = execute()
write_md(res)