-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement loading of a Sudoku Board from a file
- Loading branch information
Showing
11 changed files
with
210 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Suds -- A sudoku solver | ||
# Copyright: (C) 2022 Toshio Kuratomi | ||
# License: AGPL-3.0-or-later | ||
# See the LICENSE file for more details | ||
"""Savefile routines for suds.""" | ||
import typing as t | ||
|
||
import pydantic as p | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class LocalConfig: | ||
"""Settings for all of our models.""" | ||
extra = p.Extra.forbid | ||
|
||
|
||
class BaseModel(p.BaseModel): | ||
"""BaseModel with our preferred default configuration.""" | ||
Config = LocalConfig | ||
|
||
|
||
class BoardSchema(BaseModel): | ||
"""Structure defining a sudoku board.""" | ||
# 9 rows of 9 items containing 0 (meaning the cell is blank) through 9 | ||
rows: p.conlist( | ||
p.conlist(p.conint(gt=-1, lt=10), min_items=9, max_items=9), min_items=9, max_items=9) | ||
|
||
|
||
class SaveFileSchema(BaseModel): | ||
""" | ||
Defines the sructure of a savefile. | ||
.. note:: This is a mapping rather than a list for potential future expansion. | ||
""" | ||
board: BoardSchema | ||
|
||
|
||
# pylint: enable=too-few-public-methods | ||
|
||
|
||
def load_save_file(file: t.Union[str, t.TextIO]) -> SaveFileSchema: | ||
""" | ||
Load a SudokBoard from a savefile. | ||
:arg file: This can be a fle name or a file-like object. | ||
""" | ||
if isinstance(file, str): | ||
save_file_data = SaveFileSchema.parse_file(file) | ||
else: | ||
save_file_data = SaveFileSchema.parse_raw(file.read()) | ||
|
||
return save_file_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,26 @@ | ||
import json | ||
import sys | ||
|
||
from suds import cli | ||
|
||
good_data = [ | ||
[0, 0, 7, 0, 0, 0, 0, 0, 6], | ||
[0, 6, 0, 8, 0, 0, 2, 0, 5], | ||
[0, 0, 8, 0, 6, 9, 3, 0, 0], | ||
[7, 0, 6, 0, 3, 8, 1, 0, 0], | ||
[4, 8, 9, 0, 1, 0, 7, 6, 3], | ||
[0, 0, 3, 9, 7, 0, 5, 0, 8], | ||
[0, 0, 5, 6, 8, 0, 4, 0, 0], | ||
[8, 0, 4, 0, 0, 7, 0, 5, 0], | ||
[6, 0, 0, 0, 0, 0, 9, 0, 0], | ||
] | ||
|
||
|
||
def test_main(monkeypatch, tmpdir): | ||
tmpsavefile = tmpdir / 'save.json' | ||
with open(tmpsavefile, 'w') as f: | ||
f.write(json.dumps({'board': {'rows': good_data}})) | ||
|
||
monkeypatch.setattr(sys, 'argv', ['--filename', str(tmpsavefile)]) | ||
|
||
def test_main(): | ||
assert cli.main() == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import io | ||
import json | ||
|
||
from suds import savefile | ||
|
||
good_data = { | ||
'board': { | ||
'rows': [ | ||
[0, 0, 7, 0, 0, 0, 0, 0, 6], | ||
[0, 6, 0, 8, 0, 0, 2, 0, 5], | ||
[0, 0, 8, 0, 6, 9, 3, 0, 0], | ||
[7, 0, 6, 0, 3, 8, 1, 0, 0], | ||
[4, 8, 9, 0, 1, 0, 7, 6, 3], | ||
[0, 0, 3, 9, 7, 0, 5, 0, 8], | ||
[0, 0, 5, 6, 8, 0, 4, 0, 0], | ||
[8, 0, 4, 0, 0, 7, 0, 5, 0], | ||
[6, 0, 0, 0, 0, 0, 9, 0, 0], | ||
] | ||
} | ||
} | ||
|
||
good_data_json = json.dumps(good_data) | ||
|
||
|
||
def test_load_save_file_filename(tmpdir): | ||
tmpsavefile = tmpdir / 'save.json' | ||
with open(tmpsavefile, 'w', encoding='utf-8') as f: | ||
f.write(good_data_json) | ||
|
||
assert savefile.load_save_file(tmpsavefile) == good_data | ||
|
||
|
||
def test_load_save_file_file_obj(): | ||
file_obj = io.StringIO(good_data_json) | ||
|
||
assert savefile.load_save_file(file_obj) == good_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extra # unused variable (/home/badger/programming/playrepo/suds2/suds/savefile.py:14) | ||
Config # unused variable (/home/badger/programming/playrepo/suds2/suds/savefile.py:19) | ||
rows # unused variable (/home/badger/programming/playrepo/suds2/suds/savefile.py:25) | ||
board # unused variable (/home/badger/programming/playrepo/suds2/suds/savefile.py:36) | ||
load_save_file # unused function (/home/badger/programming/playrepo/suds2/suds/savefile.py:42) |