Skip to content

Commit 6de8944

Browse files
use pydantic settings block, extend docker compose, log routes and settings during startup; closes #69
1 parent ef702ed commit 6de8944

9 files changed

+47
-27
lines changed

Pipfile

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ black = "==24.3.0"
1515
[packages]
1616
fastapi = "==0.109.1"
1717
uvicorn = "==0.26.0"
18+
pydantic-settings = "==2.2.1"
1819

1920
[requires]
2021
python_version = "3.11"

Pipfile.lock

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ You can expect 12 entries to be solved after ~1s with `bruteforce`and <0.1s with
5656
weaker machines.
5757
Multiple cores won't speed up job time, but will enable efficient solving of parallel jobs.
5858

59-
The thresholds that decide which jobs are solved which way are defined in constants.py.
60-
This might be configurable by CLI later (TODO #69).
59+
The thresholds that decide which jobs are solved which way are defined in constants.py and can be passed as env,
60+
see [docker-compose.yml](/docker-compose.yml) for details.
6161

6262
## Contributing
6363

app/constants.py

-19
This file was deleted.

app/main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from starlette.requests import Request
77
from starlette.responses import HTMLResponse, PlainTextResponse
88

9-
from app.constants import version, solverSettings
9+
from app.settings import version, solverSettings
1010
# don't mark /app as a sources root or pycharm will delete the "app." prefix
1111
# that's needed for pytest to work correctly
1212
from app.solver.data.Job import Job
@@ -17,6 +17,8 @@
1717
@asynccontextmanager
1818
async def lifespan(app: FastAPI):
1919
print(f"Starting CutSolver {version}...")
20+
print(f"Settings: {solverSettings.json()}")
21+
print(f"Routes: {app.routes}")
2022
yield
2123
print("Shutting down CutSolver...")
2224

app/settings.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pydantic import PositiveInt
2+
from pydantic_settings import BaseSettings
3+
4+
# constant; used for git tags
5+
version = "v1.0.1"
6+
7+
8+
class SolverSettings(BaseSettings):
9+
# Desktop with Ryzen 2700X:
10+
# (4, 3, 2)=1260 => 0.1s, (4, 3, 3)=4200 => 0.8s, (5, 3, 3)=9240 => 8s
11+
bruteforce_max_combinations: PositiveInt = 5000
12+
# that is already unusable x100, but the solver takes it easily
13+
solver_n_max: PositiveInt = 2000
14+
15+
16+
# defaults can be overwritten via env
17+
solverSettings = SolverSettings()

app/solver/solver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from itertools import permutations
44
from time import perf_counter
55

6-
from app.constants import solverSettings
6+
from app.settings import solverSettings
77
from app.solver.data.Job import Job, TargetSize
88
from app.solver.data.Result import ResultLengths, Result, SolverType
99
from app.solver.utils import _get_trimming, _sorted
@@ -18,7 +18,7 @@ def solve(job: Job) -> Result:
1818
if job.n_combinations() <= solverSettings.bruteforce_max_combinations:
1919
lengths = _solve_bruteforce(job)
2020
solver_type = SolverType.bruteforce
21-
elif job.n_targets() <= solverSettings.n_max:
21+
elif job.n_targets() <= solverSettings.solver_n_max:
2222
lengths = _solve_FFD(job)
2323
solver_type = SolverType.FFD
2424
else:

docker-compose.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ services:
88
restart: unless-stopped
99
ports:
1010
- "8000:80"
11+
environment:
12+
- BRUTEFORCE_MAX_COMBINATIONS=4000
13+
- SOLVER_N_MAX=1500
1114

1215
cutsolver_frontend:
1316
image: modischfabrications/cutsolver_frontend:latest

tag_from_version.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from git import Repo
1010

11-
from app import constants
11+
from app import settings
1212

1313

1414
def compare_versions(left: str, right: str):
@@ -35,7 +35,7 @@ def process():
3535
repo = Repo(Path("."))
3636
assert not repo.bare
3737

38-
version = constants.version
38+
version = settings.version
3939

4040
version_tags_only = tuple(filter(lambda tag: tag.name[0] == "v", repo.tags))
4141
newest_tag = version_tags_only[-1]

0 commit comments

Comments
 (0)