-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
97 lines (75 loc) · 3.5 KB
/
helper.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
88
89
90
91
92
93
94
95
96
97
"""
Copyright 2020 Siddharth Priya
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import ast
from python_json_config import ConfigBuilder
from world_model import *
LINKS_CONFIG = 'Links'
CATALYSTS_CONFIG = 'Catalysts'
SUBSTRATES_CONFIG = 'Substrates'
HOLES_CONFIG = 'Holes'
DEFAULT_CONFIG = 'default'
MAX_ITER = 'max_iter'
GRID_CONFIG = 'grid_size'
DISINTEGRATE_PROB_CONFIG = 'disintegration_probability'
STR_TO_CLASS = {'Hole': Hole, 'Substrate': Substrate,
'Catalyst': Catalyst, 'Link': Link}
# This is a data object
class Config(object):
def __init__(self, grid_size, default_elem, iter, h_plist, s_plist,
k_plist, l_plist, disint_prob: float):
self.grid_size = grid_size
self.default_element = default_elem
self.iter: int = iter
self.h_plist = h_plist
self.s_plist = s_plist
self.k_plist = k_plist
self.l_plist = l_plist
self.disintegrate_prob: float = disint_prob
@staticmethod
def loadConfigFromFile(path) -> 'Config':
def makeElement(x: int, y: int, c: typing.TypeVar(T),
grid_size: int):
return c(Point(x, y), grid_size)
builder = ConfigBuilder()
config = builder.parse_config(path)
builder.set_field_access_required()
builder.add_required_field(GRID_CONFIG)
builder.add_required_field(MAX_ITER)
builder.add_required_field(DEFAULT_CONFIG)
builder.add_required_field(DISINTEGRATE_PROB_CONFIG)
builder.validate_field_type(GRID_CONFIG, int)
builder.validate_field_type(MAX_ITER, int)
builder.validate_field_type(DEFAULT_CONFIG, str)
builder.validate_field_type(HOLES_CONFIG, [(int, int)])
builder.validate_field_type(SUBSTRATES_CONFIG, [(int, int)])
builder.validate_field_type(CATALYSTS_CONFIG, [(int, int)])
builder.validate_field_type(LINKS_CONFIG, [(int, int)])
builder.validate_field_type(DISINTEGRATE_PROB_CONFIG, float)
# set up
grid_size = config.grid_size
default_element: T = STR_TO_CLASS[config.default]
iter = config.max_iter
disint_prob = config.disintegration_probability
h_plist = [makeElement(x, y, Hole, grid_size) for x, y in
list(ast.literal_eval(config.Holes))] if config.Holes else []
s_plist = [makeElement(x, y, Substrate, grid_size) for x, y in
list(ast.literal_eval(
config.Substrates))] if config.Substrates else []
k_plist = [makeElement(x, y, Catalyst, grid_size) for x, y in
list(ast.literal_eval(
config.Catalysts))] if config.Catalysts else []
l_plist = [makeElement(x, y, Link, grid_size) for x, y in
list(ast.literal_eval(config.Links))] if config.Links else []
return Config(grid_size, default_element, iter, h_plist, s_plist,
k_plist, l_plist, disint_prob)