-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
87 lines (74 loc) · 2.46 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Command line program for enumerating valid edge sets.
"""
from enumerate_edge_sets import naively_enumerate_edge_sets
import argparse
import logging
import toms_algorithm
def run_enumeration(args):
if args.toms:
print(toms_algorithm.count_valid_edge_sets(args.width, args.height))
else:
# TODO replace naive version with recursive version
valid_edge_sets = naively_enumerate_edge_sets(args.width, args.height)
if args.verbose:
c = 0
for es in valid_edge_sets:
c = c + 1 # count like a mathematician
print(f"\n{c}:\n" + es.pretty_print())
else:
# just print the number without any adornment. Makes the program more unix friendly.
print(len(list(valid_edge_sets)))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"width",
type=int,
metavar="WIDTH",
help="width of the grid",
)
parser.add_argument(
"height",
type=int,
metavar="HEIGHT",
help="height of the grid",
)
parser.add_argument(
"-t", "--toms",
action="store_true",
help="count valid edge sets using Tom's algorithm",
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="pretty print enumerated edge sets",
)
parser.add_argument(
"--profile",
action="store_true",
help="dump profiler statistics",
)
parser.add_argument(
"--loglevel",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
metavar="LEVEL",
default="WARNING",
help="logging level to emit: DEBUG, INFO, WARNING (default), ERROR",
)
args = parser.parse_args()
numeric_level = getattr(logging, args.loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % args.loglevel)
logging.basicConfig(level=numeric_level, format='%(levelname)s:%(message)s')
if args.height < 0 or args.width < 0:
raise ValueError(f"Height ({args.height}) and width ({args.width}) must be non-negative!")
logging.info(f"Enumerating valid edge sets on {args.width} x {args.height} grid")
if args.profile:
logging.info("Starting cProfile...")
import cProfile
cProfile.runctx("run_enumeration(args)", globals(), locals())
else:
run_enumeration(args)
if __name__ == '__main__':
main()