Handle PNG file generation.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle 2024-12-14 11:52:32 +01:00
parent 67f7eef636
commit 8969ea895f
4 changed files with 23 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import operator as op
from math import prod from math import prod
from typing import Any, Iterator from typing import Any, Iterator
import numpy as np
import parse # pyright: ignore[reportMissingTypeStubs] import parse # pyright: ignore[reportMissingTypeStubs]
from ..base import BaseSolver from ..base import BaseSolver
@ -64,8 +65,9 @@ class Solver(BaseSolver):
).encode(), ).encode(),
True, True,
) )
self.files.image(f"result_{rnd+1}.png", np.array(m))
yield rnd + 1 yield rnd + 1
found = True # found = True
break break
if found: if found:

View File

@ -2,6 +2,8 @@ from abc import abstractmethod
from logging import Logger from logging import Logger
from typing import Any, Final, Iterable, Iterator, Protocol, Sequence, TypeVar, overload from typing import Any, Final, Iterable, Iterator, Protocol, Sequence, TypeVar, overload
from numpy.typing import NDArray
_T = TypeVar("_T") _T = TypeVar("_T")
@ -13,9 +15,21 @@ class ProgressHandler(Protocol):
def wrap(self, values: Iterable[_T], total: int) -> Iterator[_T]: ... def wrap(self, values: Iterable[_T], total: int) -> Iterator[_T]: ...
class FileHandler(Protocol): class FileHandler:
@abstractmethod
def create(self, filename: str, content: bytes, text: bool = False): ... def create(self, filename: str, content: bytes, text: bool = False): ...
def image(self, filename: str, image: NDArray[Any]):
from io import BytesIO
import matplotlib.pyplot as plt
io = BytesIO()
plt.imsave(io, image) # type: ignore
io.seek(0)
self.create(filename, io.read(), False)
class BaseSolver: class BaseSolver:
def __init__( def __init__(

View File

@ -1,10 +1,11 @@
from pathlib import Path from pathlib import Path
from typing import Final from typing import Final
from ...base import FileHandler
from .base import dump_api_message from .base import dump_api_message
class FileHandlerAPI: class FileHandlerAPI(FileHandler):
def __init__(self, folder: Path): def __init__(self, folder: Path):
self.folder: Final = folder self.folder: Final = folder

View File

@ -2,8 +2,10 @@ import logging
from pathlib import Path from pathlib import Path
from typing import Final from typing import Final
from ..base import FileHandler
class SimpleFileHandler:
class SimpleFileHandler(FileHandler):
def __init__(self, logger: logging.Logger, folder: Path): def __init__(self, logger: logging.Logger, folder: Path):
self.logger: Final = logger self.logger: Final = logger
self.folder: Final = folder self.folder: Final = folder