-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
66 lines (57 loc) · 2.86 KB
/
run.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
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
import importlib
import time
import examples2
from gridsolver.abstract_grids.grid_loading import create_from_str_and_class, create_from_file
from gridsolver.solver import solver
from gridsolver.solver.logger import get_log, MAX_LVL as _MAX_LVL, Colouring, set_colouring
_lg = get_log(__name__, 0)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Solve grid puzzle")
parser.add_argument("-m", "--module", help="module file to load puzzle from", type=str)
parser.add_argument("-s", "--str", help="string to load puzzle from", type=str)
parser.add_argument("-c", "--class_", help="puzzle class", choices=("sudoku", "killersudoku", "futoshiki"),
type=str)
parser.add_argument("-o", "--colour", help="colour", choices=(Colouring.No, Colouring.Colorama, Colouring.Rich),
default=Colouring.Colorama, type=Colouring.__getitem__)
parser.add_argument("-f", "--file", help="puzzle string file to load puzzle from", type=str)
parser.add_argument("-e", "--example", choices=("a", "b", "c", "d", "f", "m", "s", "t"),
help="Choose one of the default example puzzles and do not load from module file",
type=str, required=False)
parser.add_argument("-d", "--detail", type=int, default=0,
help="Detail of log output (higher means more intermediate steps are shown)", required=False)
parser.add_argument("-v", "--verbose", action="store_true", help="Print very detailed log output (every step)")
args = parser.parse_args()
set_colouring(args.colour)
detail = 0
if args.detail:
detail = args.detail
if args.verbose:
detail = _MAX_LVL
solver.set_loglevel(detail)
start_time = time.process_time()
if args.file and args.example:
raise ValueError("Cannot choose from file and example.")
if args.file and args.str:
raise ValueError("Cannot choose from file and string.")
if args.example and args.str:
raise ValueError("Cannot choose from string and example.")
if args.module:
_lg.logs(0, f"Importing grid puzzle from {args.module}")
x = importlib.import_module(args.module)
assert hasattr(x, "g"), "Module does not define puzzle object g"
solver.solve(x.g)
elif args.file:
g = create_from_file(args.file)
_lg.logs(0, f"Importing grid puzzle from {args.file}")
solver.solve(g)
elif args.example:
g = examples2.get_example(args)
solver.solve(g)
elif args.str:
g = create_from_str_and_class(args.str, args.class_)
solver.solve(g)
else:
raise RuntimeError("Must define input puzzle either via module file or example choice. Run -h to see details.")
elapsed_time = time.process_time() - start_time
_lg.logs(0, f"Took {elapsed_time:.4f}s to execute.")