Skip to content

Commit

Permalink
Rename python package
Browse files Browse the repository at this point in the history
  • Loading branch information
jsemric committed Dec 7, 2024
1 parent b2234f8 commit 1488bca
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ x-common:
services:
server:
<<: *common
command: ["uvicorn", "src.app:create_app", "--port", "8000", "--host", "0.0.0.0"]
command: ["uvicorn", "gomoku.app:create_app", "--port", "8000", "--host", "0.0.0.0"]
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost:8000/healthz"]
interval: 10s
Expand Down
9 changes: 5 additions & 4 deletions server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ COPY --chown=apiuser:apiuser requirements.txt .
RUN pip install --upgrade pip && \
pip install -r requirements.txt && \
rm -rf .cache
COPY --chown=apiuser:apiuser src ./src
COPY --chown=apiuser:apiuser setup.py ./
COPY --chown=apiuser:apiuser gomoku ./gomoku
COPY --chown=apiuser:apiuser tests ./tests
RUN cd src && python setup.py build_ext --inplace
ENV PYTHONPATH=/home/apiuser/src
RUN python setup.py build_ext
ENV PYTHONPATH=/home/apiuser/gomoku
EXPOSE 8000
CMD ["uvicorn", "src.app:create_app", "--port", "8000", "--host", "0.0.0.0"]
CMD ["uvicorn", "gomoku.app:create_app", "--port", "8000", "--host", "0.0.0.0"]
10 changes: 5 additions & 5 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ IMAGE=server
PYTHON_BIN?=python3

build:
@cd src && $(PYTHON_BIN) setup.py build_ext --inplace
@cd gomoku && $(PYTHON_BIN) setup.py build_ext --inplace

start:
PYTHONPATH=src LOG_LEVEL=DEBUG $(PYTHON_BIN) -m uvicorn --host 0.0.0.0 --port 3001 --factory "src.app:create_app" --reload
PYTHONPATH=gomoku LOG_LEVEL=DEBUG $(PYTHON_BIN) -m uvicorn --host 0.0.0.0 --port 3001 --factory "gomoku.app:create_app" --reload

fmt:
@$(PYTHON_BIN) -m black src tests
@$(PYTHON_BIN) -m black gomoku tests

test: build
@$(PYTHON_BIN) -m coverage run --source src --omit src/setup.py -m pytest -o log_cli=false --doctest-modules
@$(PYTHON_BIN) -m coverage run --source gomoku --omit gomoku/setup.py -m pytest -o log_cli=false --doctest-modules
@$(PYTHON_BIN) -m coverage report
@$(PYTHON_BIN) -m coverage html

clean:
@rm -fr tests/.pytest_cache .pytest_cache src/build src/*.so src/*.c htmlcov .coverage
@rm -fr tests/.pytest_cache .pytest_cache gomoku/build gomoku/*.so gomoku/*.c htmlcov .coverage
4 changes: 2 additions & 2 deletions server/src/app.py → server/gomoku/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

from strategy import Mtd
from game import validate, GameError, GameStatus, GameState
from gomoku.strategy import Mtd
from gomoku.game import validate, GameError, GameStatus, GameState

HEALTH_ENDPOINTS = ["/", "/healthz"]
logger = logging.getLogger(__name__)
Expand Down
3 changes: 2 additions & 1 deletion server/src/game.py → server/gomoku/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from dataclasses import dataclass
from enum import Enum
from typing import Optional, Generator
from utils import GRID_SIZE, inside_interval_pos, check_winning_step

from gomoku.utils import GRID_SIZE, inside_interval_pos, check_winning_step


class GameStatus(str, Enum):
Expand Down
25 changes: 10 additions & 15 deletions server/src/strategy.py → server/gomoku/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import math
from typing import Optional
from abc import ABC, abstractmethod
from game import GameState
from timer import timeit

from utils import (
from gomoku.game import GameState
from gomoku.utils import (
GRID_SIZE,
steps_to_win,
get_neigh,
Expand Down Expand Up @@ -58,7 +57,7 @@ def __init__(

def run(self, gs: GameState) -> Optional[int]:
self.next_pos = increment_cell_pos(self.next_pos, self.row_inc, self.col_inc)
if gs.is_taken(self.next_pos) or not inside_interval_pos(self.next_pos):
if not inside_interval_pos(self.next_pos) or gs.is_taken(self.next_pos):
self.next_pos = _random_cell(gs)
return self.next_pos

Expand Down Expand Up @@ -101,10 +100,10 @@ def _memorized_search(
alpha: float = -math.inf,
beta: float = math.inf,
) -> tuple[Optional[int], float]:
transp_key = gs.grid.tobytes(), depth, maximize
key = gs.grid.tobytes(), depth, maximize
move, lower_bound, upper_bound = -1, -math.inf, math.inf
if transp_key in self._memory:
move, lower_bound, upper_bound = self._memory[transp_key]
if key in self._memory:
move, lower_bound, upper_bound = self._memory[key]
self._memory_hits += 1
if lower_bound >= beta:
return move, lower_bound
Expand All @@ -118,11 +117,11 @@ def _memorized_search(
best_move, best_score = self._search(gs, depth, maximize, alpha, beta)

if best_score <= alpha:
self._memory[transp_key] = best_move, lower_bound, best_score
self._memory[key] = best_move, lower_bound, best_score
if beta > best_score > alpha:
self._memory[transp_key] = best_move, best_score, best_score
self._memory[key] = best_move, best_score, best_score
if best_score >= beta:
self._memory[transp_key] = best_move, best_score, upper_bound
self._memory[key] = best_move, best_score, upper_bound
return best_move, best_score

def _search(
Expand All @@ -137,11 +136,7 @@ def _search(
assert gs.grid[gs.last_step] == gs.last_player

# check other player victory
score = self._evaluate(
gs,
not maximize,
depth,
)
score = self._evaluate(gs, not maximize, depth)
if score is not None:
return None, score

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion server/src/setup.py → server/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
ext_modules=cythonize(
["utils.pyx"],
["gomoku/utils.pyx"],
compiler_directives={"language_level": "3"},
)
)
18 changes: 0 additions & 18 deletions server/src/timer.py

This file was deleted.

4 changes: 2 additions & 2 deletions server/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from fastapi.testclient import TestClient

from game import GameStatus
from app import create_app, NextMoveRequest
from gomoku.game import GameStatus
from gomoku.app import create_app, NextMoveRequest


@pytest.fixture
Expand Down
8 changes: 4 additions & 4 deletions server/tests/test_strategy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
import itertools

from game import GameState, GameStatus
from strategy import Mtd, AlphaBeta, RandomStrategy, DummyStrategy, BaseStrategy
from gomoku.game import GameState, GameStatus
from gomoku.strategy import Mtd, AlphaBeta, RandomStrategy, DummyStrategy, BaseStrategy

CELL_INCREMENTS = set(itertools.permutations([-1, -1, 0, 1, 1], 2))

Expand All @@ -16,10 +16,10 @@ def test_alphabeta_strategy_beats_random():
_second_strategy_wins(RandomStrategy(), AlphaBeta(3), 20)


@pytest.mark.skip("Revisit later")
@pytest.mark.skip("Slow test")
@pytest.mark.parametrize("row_inc, col_inc", CELL_INCREMENTS)
def test_alphabeta_strategy_beats_dummy(row_inc, col_inc):
_second_strategy_wins(DummyStrategy(row_inc, col_inc), AlphaBeta(5), 20)
_second_strategy_wins(DummyStrategy(row_inc, col_inc), AlphaBeta(4), 20)


def test_mtd_strategy_beats_random():
Expand Down
2 changes: 1 addition & 1 deletion server/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import math

from utils import (
from gomoku.utils import (
steps_to_win,
check_winning_step,
GRID_ROWS,
Expand Down

0 comments on commit 1488bca

Please sign in to comment.