Skip to content

Commit

Permalink
Merge pull request #153 from wimglenn/2.1
Browse files Browse the repository at this point in the history
aocd.extra attr
  • Loading branch information
wimglenn authored Dec 21, 2024
2 parents 24c09ce + c2d39fb commit 89b8698
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions aocd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import json
import sys
import typing as t
from functools import partial
Expand Down Expand Up @@ -26,6 +28,7 @@
"data",
"examples",
"exceptions",
"extra",
"get",
"get_data",
"models",
Expand All @@ -39,6 +42,7 @@

if t.TYPE_CHECKING:
data: str
extra: dict[str, t.Any]
puzzle: models.Puzzle
submit = _impartial_submit

Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions aocd/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
7 changes: 6 additions & 1 deletion aocd/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import itertools
import json
import logging
import os
import sys
Expand Down Expand Up @@ -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__)


Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +20,7 @@ dependencies = [
"pebble",
"urllib3",
'tzdata ; platform_system == "Windows"',
"aocd-example-parser >= 2023.2",
"aocd-example-parser >= 2023.12.21",
]

[[project.authors]]
Expand Down
6 changes: 6 additions & 0 deletions tests/test_aocd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 89b8698

Please sign in to comment.