Skip to content

Commit cf6ebb4

Browse files
authored
Merge pull request #121 from ImogenBits/program_rework
Internal rework
2 parents 6c5fb4b + 58e77c1 commit cf6ebb4

11 files changed

Lines changed: 1308 additions & 1479 deletions

File tree

algobattle/battle.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
Callable,
1515
ClassVar,
1616
Concatenate,
17-
Hashable,
1817
Literal,
1918
ParamSpec,
2019
Protocol,
@@ -24,11 +23,10 @@
2423
)
2524

2625
from pydantic import Field, GetCoreSchemaHandler
27-
from pydantic.main import BaseModel as PydanticBase
2826
from pydantic_core import CoreSchema
2927
from pydantic_core.core_schema import tagged_union_schema
3028

31-
from algobattle.docker_util import (
29+
from algobattle.program import (
3230
Generator,
3331
ProgramRunInfo,
3432
ProgramUi,
@@ -229,13 +227,13 @@ class Config(BaseModel):
229227
:meth:`Battle.run` method with its fields set accordingly.
230228
"""
231229

232-
type: str = "Iterated"
230+
type: str
233231
"""Type of battle that will be used."""
234232

235233
@classmethod
236-
def __get_pydantic_core_schema__(cls, source: Type[PydanticBase], handler: GetCoreSchemaHandler) -> CoreSchema:
234+
def __get_pydantic_core_schema__(cls, source: Type, handler: GetCoreSchemaHandler) -> CoreSchema:
237235
# there's two bugs we need to catch:
238-
# 1. this function is called during the pydantic BaseModel metaclass's __new__, so the Battle class
236+
# 1. this function is called during the pydantic BaseModel metaclass's __new__, so the BattleConfig class
239237
# won't be ready at that point and be missing in the namespace
240238
# 2. pydantic uses the core schema to build child classes core schema. for them we want to behave like a
241239
# normal model, only our own schema gets modified
@@ -244,17 +242,19 @@ def __get_pydantic_core_schema__(cls, source: Type[PydanticBase], handler: GetCo
244242
return handler(source)
245243
except NameError:
246244
return handler(source)
247-
battle_classes = Battle.all()
248-
match len(battle_classes):
245+
match len(Battle._battle_types):
249246
case 0:
250247
return handler(source)
251248
case 1:
252-
return handler(next(iter(battle_classes.values())).Config)
249+
return handler(next(iter(Battle._battle_types.values())))
253250
case _:
254-
choices: dict[Hashable, CoreSchema] = {
255-
name: handler(sublass.Config) for name, sublass in battle_classes.items()
256-
}
257-
return tagged_union_schema(choices=choices, discriminator="type")
251+
return tagged_union_schema(
252+
choices={
253+
battle.Config.model_fields["type"].default: battle.Config.__pydantic_core_schema__
254+
for battle in Battle._battle_types.values()
255+
},
256+
discriminator="type",
257+
)
258258

259259
class UiData(BaseModel):
260260
"""Object containing custom diplay data.

algobattle/cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
from anyio.abc import TaskGroup
1818

1919
from algobattle.battle import Battle, Fight
20-
from algobattle.match import BaseConfig, Match, Ui
20+
from algobattle.match import Match, Ui, AlgobattleConfig
2121
from algobattle.problem import AnyProblem, Problem
22-
from algobattle.team import Matchup
2322
from algobattle.util import Role, RunningTimer, flat_intersperse
23+
from algobattle.program import Matchup
2424

2525

2626
@dataclass
@@ -32,7 +32,7 @@ class CliOptions:
3232
result: Path | None = None
3333

3434

35-
def parse_cli_args(args: list[str]) -> tuple[CliOptions, BaseConfig]:
35+
def parse_cli_args(args: list[str]) -> tuple[CliOptions, AlgobattleConfig]:
3636
"""Parse a given CLI arg list into config objects."""
3737
parser = ArgumentParser()
3838
parser.add_argument(
@@ -52,8 +52,8 @@ def parse_cli_args(args: list[str]) -> tuple[CliOptions, BaseConfig]:
5252
if path.is_dir():
5353
path /= "config.toml"
5454

55-
config = BaseConfig.from_file(path)
56-
problem = Problem.get(config.match.problem)
55+
config = AlgobattleConfig.from_file(path)
56+
problem = Problem.load(config.match.problem)
5757

5858
exec_config = CliOptions(
5959
problem=problem,
@@ -65,7 +65,7 @@ def parse_cli_args(args: list[str]) -> tuple[CliOptions, BaseConfig]:
6565

6666

6767
async def _run_with_ui(
68-
match_config: BaseConfig,
68+
match_config: AlgobattleConfig,
6969
problem: AnyProblem,
7070
) -> Match:
7171
async with CliUi() as ui:

0 commit comments

Comments
 (0)