Refactoring
This commit is contained in:
parent
2225ae6b40
commit
e791a1e82d
@ -28,43 +28,70 @@ def _file_stem(f):
|
|||||||
return f[-5:]
|
return f[-5:]
|
||||||
|
|
||||||
def get_problems(path=None, ext=None):
|
def get_problems(path=None, ext=None):
|
||||||
|
wd = os.getcwd()
|
||||||
os.chdir(Path(path))
|
os.chdir(Path(path))
|
||||||
return sorted([script.as_posix() for script in Path(".").rglob(f"*[0-9].{ext}")], key=_file_stem)
|
scripts = sorted([script.as_posix() for script in Path(".").rglob(f"*[0-9].{ext}")], key=_file_stem)
|
||||||
|
os.chdir(wd)
|
||||||
def execute():
|
return scripts
|
||||||
"""
|
|
||||||
Execute script and return output
|
|
||||||
"""
|
|
||||||
# scripts = get_problems(path="Python", ext="py")
|
|
||||||
scripts = get_problems(path="Julia", ext="jl")
|
|
||||||
# print(scripts[:5])
|
|
||||||
|
|
||||||
|
def timing_Python(path=None):
|
||||||
|
wd = os.getcwd()
|
||||||
|
scripts = get_problems(path=path, ext="py")
|
||||||
|
os.chdir(Path(path))
|
||||||
python_timings, python_results = [], []
|
python_timings, python_results = [], []
|
||||||
|
for script in scripts[:5]:
|
||||||
|
_res = run(["python3", f"{script}"], capture_output=True, text=True).stdout.split("\n")
|
||||||
|
python_timings.append(_res[0])
|
||||||
|
python_results.append(_res[2])
|
||||||
|
|
||||||
|
os.chdir(wd)
|
||||||
|
return python_timings, python_results
|
||||||
|
|
||||||
|
def timing_Julia(path=None):
|
||||||
|
wd = os.getcwd()
|
||||||
|
scripts = get_problems(path="Julia", ext="jl")
|
||||||
|
os.chdir(Path(path))
|
||||||
julia_problem_numbers, julia_timings, julia_results = [], [], []
|
julia_problem_numbers, julia_timings, julia_results = [], [], []
|
||||||
for script in scripts[:5]:
|
for script in scripts[:5]:
|
||||||
# _res = run(["python3", f"{script}"], capture_output=True, text=True).stdout.split("\n")
|
|
||||||
# python_timings.append(_res[0])
|
|
||||||
# python_results.append(_res[2])
|
|
||||||
_res = run(["julia", f"{script}"], capture_output=True, text=True).stdout.split("\n")
|
_res = run(["julia", f"{script}"], capture_output=True, text=True).stdout.split("\n")
|
||||||
julia_problem_numbers.append(_res[0])
|
julia_problem_numbers.append(_res[0])
|
||||||
julia_timings.append(_res[1].split("(")[0])
|
julia_timings.append(_res[1].split("(")[0])
|
||||||
julia_results.append(_res[3])
|
julia_results.append(_res[3])
|
||||||
|
|
||||||
writer = MarkdownTableWriter(
|
os.chdir(wd)
|
||||||
table_name="Project Euler Solutions",
|
return julia_problem_numbers, julia_timings, julia_results
|
||||||
# headers=["Problem #", "Result", "Execution time (Python)"],
|
|
||||||
# value_matrix=[[f"{int(t[25:28]):03d}", int(r[22:]), t[29:]] for t,r in zip(python_timings, python_results)],
|
def execute():
|
||||||
headers=["Problem #", "Result", "Execution time (Julia)"],
|
"""
|
||||||
value_matrix=[[f"{int(p[-5:-1]):03d}", int(r[23:]), t] for p,t,r in zip(julia_problem_numbers, julia_timings, julia_results)],
|
Execute scripts and return md formatted table
|
||||||
column_styles=[
|
"""
|
||||||
Style(align="center"),
|
python_timings, python_results = timing_Python(path="Python")
|
||||||
Style(align="right", thousand_separator="_"),
|
julia_problem_numbers, julia_timings, julia_results = timing_Julia(path="Julia")
|
||||||
Style(align="right"),
|
|
||||||
], # specify styles for each column
|
for lang in ["Python", "Julia"]:
|
||||||
margin=1 # add a whitespace for both sides of each cell
|
if lang == "Python":
|
||||||
)
|
headers=["Problem #", "Result", "Execution time (Python)"]
|
||||||
writer.write_table()
|
value_matrix=[[f"{int(t[25:28]):03d}", int(r[22:]), t[29:]] for t,r in zip(python_timings, python_results)]
|
||||||
return writer.dumps()
|
elif lang == "Julia":
|
||||||
|
headers=["Problem #", "Result", "Execution time (Julia)"]
|
||||||
|
value_matrix=[[f"{int(p[-5:-1]):03d}", int(r[23:]), t] for p,t,r in zip(julia_problem_numbers, julia_timings, julia_results)]
|
||||||
|
print(f"Printing for {lang}")
|
||||||
|
writer = MarkdownTableWriter(
|
||||||
|
table_name="Project Euler Solutions",
|
||||||
|
headers=headers, #["Problem #", "Result", f"Execution time ({lang})"],
|
||||||
|
value_matrix=value_matrix, #[[f"{int(t[25:28]):03d}", int(r[22:]), t[29:]] for t,r in zip(python_timings, python_results)],
|
||||||
|
# headers=["Problem #", "Result", "Execution time (Julia)"],
|
||||||
|
# value_matrix=[[f"{int(p[-5:-1]):03d}", int(r[23:]), t] for p,t,r in zip(julia_problem_numbers, julia_timings, julia_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()
|
||||||
|
print(writer.dumps())
|
||||||
|
# return writer.dumps()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user