-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsdk_reader.py
57 lines (46 loc) · 1.37 KB
/
sdk_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Reading and writing Sudoku boards. We use the minimal
subset of the SadMan Sudoku ".sdk" format,
see http://www.sadmansoftware.com/sudoku/faq19.php
Author: M Young, January 2018
"""
import sdk_board
from sdk_config import NROWS
from typing import List, Union
from io import IOBase
import logging
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
class InputError(Exception):
pass
def read(f: Union[IOBase, str],
board: sdk_board.Board=None) -> sdk_board.Board:
"""Read a Sudoku board from a file. Pass in a path
or an already opened file. Optionally pass in a board to be
filled.
"""
if isinstance(f, str):
log.debug("Reading from string")
f = open(f, "r")
else:
log.debug(f"Reading from file {f}")
if board is None:
board = sdk_board.Board()
values = []
for row in f:
row = row.strip()
if len(row) == 0:
continue
log.debug(f"Reading row |{row}|")
values.append(row)
if len(row) != NROWS:
raise InputError("Puzzle row wrong length: {}"
.format(row))
log.debug(f"Read values: {values}")
if len(values) != NROWS:
raise InputError("Wrong number of rows in {}"
.format(values))
board.set_tiles(values)
f.close()
return board