From ae703f98aabec4eb20cbe39814f2f9b795db8b36 Mon Sep 17 00:00:00 2001 From: AKP Date: Wed, 21 Dec 2022 17:12:28 +0000 Subject: [PATCH] Code formatting Signed-off-by: AKP --- generate-benchmark-graph.py | 48 ++++++++++++------- lib/aocpy/__init__.py | 17 ++++--- lib/aocpy/vis.py | 5 +- runtime/runners/interface/python.py | 22 ++++++--- .../py/__init__.py | 2 +- 5 files changed, 61 insertions(+), 33 deletions(-) diff --git a/generate-benchmark-graph.py b/generate-benchmark-graph.py index 6b19304..fb8ef35 100644 --- a/generate-benchmark-graph.py +++ b/generate-benchmark-graph.py @@ -10,11 +10,7 @@ import re OUTPUT_FILE = sys.argv[1] YEAR = sys.argv[2] -COLOURS = { - "Golang": "#00ADD8", - "Python": "#3572A5", - "Nim": "#ffc200" -} +COLOURS = {"Golang": "#00ADD8", "Python": "#3572A5", "Nim": "#ffc200"} MAX_Y_VALUE = 1 @@ -23,12 +19,18 @@ challenge_dir_regex = re.compile("""(?m)^(\d{2})-([a-zA-Z]+)$""") directories = [] path = os.path.join("challenges", YEAR) for filename in os.listdir(path): - if os.path.isdir(os.path.join(path, filename)) and challenge_dir_regex.match(filename): + if os.path.isdir(os.path.join(path, filename)) and challenge_dir_regex.match( + filename + ): directories.append(filename) files = [os.path.join(x, "benchmark.json") for x in directories] -benchmark_data = {"Python": {}, "Golang": {}, "Nim": {}} # adding dicts here sets the order of points being plotted +benchmark_data = { + "Python": {}, + "Golang": {}, + "Nim": {}, +} # adding dicts here sets the order of points being plotted for filename in files: fpath = os.path.join(path, filename) @@ -37,7 +39,7 @@ for filename in files: except FileNotFoundError: print(f"Warning: missing file {fpath}") continue - + data = json.load(f) f.close() @@ -54,7 +56,7 @@ for lang in benchmark_data: day = int(key.split(".", 1)[0]) all_days.add(day) -figure = plt.figure(figsize=(25/2, 5)) +figure = plt.figure(figsize=(25 / 2, 5)) axp1 = figure.add_subplot(1, 2, 1) axp2 = figure.add_subplot(1, 2, 2, sharey=axp1) @@ -69,7 +71,7 @@ for i, language in enumerate(benchmark_data): days = [] for key in data: - + day = int(key.split(".", 1)[0]) if day not in days: days.append(day) @@ -78,7 +80,7 @@ for i, language in enumerate(benchmark_data): part_one_times.append(data[key]) if key.endswith(".2"): part_two_times.append(data[key]) - + colour = COLOURS.get(language) p1 = axp1.scatter(days, part_one_times, color=colour) @@ -87,26 +89,40 @@ for i, language in enumerate(benchmark_data): for i, day in enumerate(days): if i + 1 >= len(days): continue - if days[i+1] == day+1: - axp1.plot((day, days[i+1]), (part_one_times[i], part_one_times[i+1]), "-", color=colour) - axp2.plot((day, days[i+1]), (part_two_times[i], part_two_times[i+1]), "-", color=colour) + if days[i + 1] == day + 1: + axp1.plot( + (day, days[i + 1]), + (part_one_times[i], part_one_times[i + 1]), + "-", + color=colour, + ) + axp2.plot( + (day, days[i + 1]), + (part_two_times[i], part_two_times[i + 1]), + "-", + color=colour, + ) figure.suptitle(f"Average {YEAR} challenge running time") axp1.set_title("Part one") axp2.set_title("Part two") + def do_auxillary_parts(axis): plt.sca(axis) plt.xticks(list(all_days), [str(y) for y in all_days]) plt.ylabel("Running time (log seconds)") plt.yscale("log") plt.xlabel("Day") - plt.legend(handles=[patches.Patch(color=COLOURS[label], label=label) for label in COLOURS]) + plt.legend( + handles=[patches.Patch(color=COLOURS[label], label=label) for label in COLOURS] + ) # plt.ylim([0, MAX_Y_VALUE]) # plt.legend(legends) + do_auxillary_parts(axp1) do_auxillary_parts(axp2) plt.tight_layout() -plt.savefig(OUTPUT_FILE) \ No newline at end of file +plt.savefig(OUTPUT_FILE) diff --git a/lib/aocpy/__init__.py b/lib/aocpy/__init__.py index b81c574..59b07d6 100644 --- a/lib/aocpy/__init__.py +++ b/lib/aocpy/__init__.py @@ -4,7 +4,6 @@ from collections.abc import Sequence class BaseChallenge: - @staticmethod def one(instr: str) -> Any: raise NotImplementedError @@ -21,12 +20,14 @@ class BaseChallenge: T = TypeVar("T") U = TypeVar("U") + def foldl(p: Callable[[U, T], U], i: Iterable[T], start: U) -> U: res = start for item in i: res = p(res, item) return res + def foldr(p: Callable[[U, T], U], i: Iterable[T], start: U) -> U: return foldl(p, reversed(i), start) @@ -42,13 +43,15 @@ class Vector: return ValueError("expected integer tuple or pair of integers") else: x, y = args - + self.x = int(x) self.y = int(y) @staticmethod def _is_vector_tuple(o: Any) -> bool: - return type(o) == tuple and len(o) == 2 and type(o[0]) == int and type(o[1]) == int + return ( + type(o) == tuple and len(o) == 2 and type(o[0]) == int and type(o[1]) == int + ) def manhattan_distance(self, o: Vector) -> int: return abs(self.x - o.x) + abs(self.y - o.y) @@ -84,6 +87,7 @@ class Vector: def __hash__(self): return hash((self.x, self.y)) + class Consumer: x: Sequence[T] i: int @@ -91,14 +95,15 @@ class Consumer: def __init__(self, x: Sequence[T]): self.x = x self.i = 0 - + def take(self) -> T: self.i += 1 - return self.x[self.i-1] + return self.x[self.i - 1] def undo(self): self.i -= 1 + class RepeatingConsumer(Consumer): def take(self) -> T: val = super().take() @@ -108,4 +113,4 @@ class RepeatingConsumer(Consumer): def undo(self): super().undo() if self.i < 0: - self.i += len(self.x) \ No newline at end of file + self.i += len(self.x) diff --git a/lib/aocpy/vis.py b/lib/aocpy/vis.py index 8d4a2d2..c568bca 100644 --- a/lib/aocpy/vis.py +++ b/lib/aocpy/vis.py @@ -1,6 +1,7 @@ import os.path -class SaveManager(): + +class SaveManager: def __init__(self, d): self.dir = d self.current_n = 0 @@ -8,5 +9,3 @@ class SaveManager(): def save(self, im): im.save(os.path.join(self.dir, f"frame_{str(self.current_n).zfill(4)}.png")) self.current_n += 1 - - \ No newline at end of file diff --git a/runtime/runners/interface/python.py b/runtime/runners/interface/python.py index e81dd7e..82c5a02 100644 --- a/runtime/runners/interface/python.py +++ b/runtime/runners/interface/python.py @@ -6,13 +6,20 @@ import json # TASKS_STR = input() # TASKS = json.loads(TASKS_STR) + def send_result(task_id, ok, output, duration): - print(json.dumps({ - "task_id": task_id, - "ok": ok, - "output": str(output) if output is not None else "", - "duration": float(duration), - }), flush=True) + print( + json.dumps( + { + "task_id": task_id, + "ok": ok, + "output": str(output) if output is not None else "", + "duration": float(duration), + } + ), + flush=True, + ) + while True: task = json.loads(input()) @@ -39,10 +46,11 @@ while True: except Exception as e: err = f"{type(e)}: {e}" import traceback + err = f"{type(e)}: {e}\n{''.join(traceback.format_tb(e.__traceback__))}" end_time = time.time() - running_time = end_time-start_time + running_time = end_time - start_time if err is not None: send_result(task_id, False, err, running_time) diff --git a/template/{{cookiecutter.__formattedDayNumber}}-{{cookiecutter.__camelTitle}}/py/__init__.py b/template/{{cookiecutter.__formattedDayNumber}}-{{cookiecutter.__camelTitle}}/py/__init__.py index 9a18694..885d677 100644 --- a/template/{{cookiecutter.__formattedDayNumber}}-{{cookiecutter.__camelTitle}}/py/__init__.py +++ b/template/{{cookiecutter.__formattedDayNumber}}-{{cookiecutter.__camelTitle}}/py/__init__.py @@ -1,8 +1,8 @@ from typing import * from aocpy import BaseChallenge -class Challenge(BaseChallenge): +class Challenge(BaseChallenge): @staticmethod def one(instr: str) -> int: raise NotImplementedError