From d07eb5054cb705ad5e520be74632a0ad0a9fb3b3 Mon Sep 17 00:00:00 2001 From: Wim Jeantine-Glenn Date: Fri, 13 Dec 2024 01:48:12 -0600 Subject: [PATCH 1/2] aocd.extra attr --- aocd/__init__.py | 6 ++++++ aocd/examples.py | 4 ++-- aocd/runner.py | 7 ++++++- pyproject.toml | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/aocd/__init__.py b/aocd/__init__.py index ae5b683..7fc7274 100644 --- a/aocd/__init__.py +++ b/aocd/__init__.py @@ -1,3 +1,5 @@ +import os +import json import sys import typing as t from functools import partial @@ -26,6 +28,7 @@ "data", "examples", "exceptions", + "extra", "get", "get_data", "models", @@ -39,6 +42,7 @@ if t.TYPE_CHECKING: data: str + extra: dict[str, t.Any] puzzle: models.Puzzle submit = _impartial_submit @@ -50,6 +54,8 @@ def __getattr__(name: str) -> t.Any: if name == "puzzle": day, year = get_day_and_year() return get_puzzle(day=day, year=year) + if name == "extra": + return json.loads(os.environ.get("AOCD_EXTRA", "{}")) if name == "submit": try: day, year = get_day_and_year() diff --git a/aocd/examples.py b/aocd/examples.py index a987e85..ed25ef0 100644 --- a/aocd/examples.py +++ b/aocd/examples.py @@ -121,13 +121,13 @@ class Example(NamedTuple): Sometimes examples in the prose need some extra context, such as a fewer number of iterations to be used when working with the test data. This may - be returned as some human-readable string in `example.extra` + be returned as key/val context in `example.extra`. """ input_data: str answer_a: str | None = None answer_b: str | None = None - extra: str | None = None + extra: dict[str, t.Any] | None = None @property def answers(self) -> tuple[str | None, str | None]: diff --git a/aocd/runner.py b/aocd/runner.py index 586fffb..6902fde 100644 --- a/aocd/runner.py +++ b/aocd/runner.py @@ -2,6 +2,7 @@ import contextlib import itertools +import json import logging import os import sys @@ -30,7 +31,7 @@ # every problem has a solution that completes in at most 15 seconds on ten-year-old hardware -DEFAULT_TIMEOUT = 60 +DEFAULT_TIMEOUT: float = 60. log: logging.Logger = logging.getLogger(__name__) @@ -346,6 +347,9 @@ def run_for( for dataset in datas: if example: data = examples[dataset].input_data + extra = examples[dataset].extra + if extra: + os.environ[f"AOCD_EXTRA"] = json.dumps(extra) else: token = datasets[dataset] os.environ["AOC_SESSION"] = token @@ -363,6 +367,7 @@ def run_for( progress=progress, capture=capture, ) + os.environ.pop(f"AOCD_EXTRA", None) runtime = format_time(walltime, timeout) line = " ".join([runtime, progress]) if error: diff --git a/pyproject.toml b/pyproject.toml index a0bda70..7d6f93d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "advent-of-code-data" -version = "2.0.4" +version = "2.1.0" description = "Get your puzzle data with a single import" readme = "README.md" requires-python = ">=3.9" @@ -20,7 +20,7 @@ dependencies = [ "pebble", "urllib3", 'tzdata ; platform_system == "Windows"', - "aocd-example-parser >= 2023.2", + "aocd-example-parser >= 2023.12.21", ] [[project.authors]] From c2d39fbb4834ca114a85a0d3990ee7d288ea4a70 Mon Sep 17 00:00:00 2001 From: Wim Jeantine-Glenn Date: Fri, 20 Dec 2024 21:07:32 -0600 Subject: [PATCH 2/2] extra test --- tests/test_aocd.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_aocd.py b/tests/test_aocd.py index 0703769..fd82a37 100644 --- a/tests/test_aocd.py +++ b/tests/test_aocd.py @@ -63,3 +63,9 @@ def test_import_puzzle(mocker): assert puzzle.year == 2023 assert puzzle.day == 21 assert puzzle.input_data == "test puzzle" + + +def test_extra_context(monkeypatch): + monkeypatch.setenv("AOCD_EXTRA", '{"k": "v"}') + from aocd import extra + assert extra["k"] == "v"