-
Notifications
You must be signed in to change notification settings - Fork 0
/
File_writer.py
140 lines (125 loc) · 4.77 KB
/
File_writer.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from basic_tools import is_dict, is_number
from syntax import All_operator_names, Auxiliar_directives
from syntax import directives_that_comunicate_an_organism_with_its_environment
from syntax import no_effect_directives
from copy import deepcopy
import os
import shutil
import pickle
def get_skeleton_of_settings(settings, final_element):
if is_dict(settings):
result = {}
for item in settings:
if item in (
All_operator_names +
Auxiliar_directives +
Directives_that_comunicate_an_organism_with_the_environment +
[
'literal',
'infinity',
'initial value',
'initial value #x #y',
'value after updating',
'value after updating #x #y',
'update once every',
'value in next cycle',
'value after mutation',
'allowed interval',
'type of inputs',
'type of outputs',
'output function',
'check number of inputs',
'check inputs',
'matrix size'
]
):
return final_element
elif item not in no_effect_directives:
result[item] = get_skeleton_of_settings(
settings[item],
final_element)
return result
else:
return final_element
"""
When we change the settings of the ecosystem, we can
execute this line to write the dictionary of
elements_to_store:
print_dictionary(get_skeleton_of_settings(
my_example_of_ecosystem_settings,
'Once'),
'Elements_to_store')
"""
def store(element, file):
"""
This method stores the element in the file
"""
pass
class DataStorer:
def __init__(self, parent_ecosystem, elements_to_store):
self.parent_ecosystem = parent_ecosystem
self.elements_to_store = elements_to_store
if os.path.isdir('historic'):
shutil.rmtree('historic')
os.mkdir('historic')
def store_data(self):
# print "Storing data..."
def check(*kew_words):
# This checks if it's time to store the
# element referenced with kew_words
code = self.elements_to_store
for kew_word in kew_words:
if is_dict(code) and kew_word in code:
code = code[kew_word]
else:
return False
return (
(
code == 'Once' and self.parent_ecosystem.time == 0
) or
(
is_number(code) and
round(self.parent_ecosystem.time / code)
== self.parent_ecosystem.time / code
)
)
current_data = {
'biotope': {
'biotope features': {}
},
'ecosystem features': {},
'organisms list': [],
}
if check('biotope', 'size'):
current_data['biotope']['size'] = deepcopy(
self.parent_ecosystem.biotope['size'])
if (
'biotope' in self.elements_to_store and
'biotope features' in self.elements_to_store['biotope']
):
for feature in (
self.elements_to_store['biotope']['biotope features']
):
if check('biotope', 'biotope features', feature):
current_data['biotope']['biotope features'][
feature] = deepcopy(
self.parent_ecosystem.biotope.biotope_features[
feature].current_value
)
if 'ecosystem features' in self.elements_to_store:
for feature in self.elements_to_store['ecosystem features']:
if check('ecosystem features', feature):
current_data['ecosystem features'][feature] = deepcopy(
self.parent_ecosystem.ecosystem_features[
feature].current_value
)
for organism in self.parent_ecosystem.organisms_list:
data = {}
if 'genes' in self.elements_to_store:
for gene in self.elements_to_store['genes']:
if check('genes', gene):
data[gene] = deepcopy(organism[gene])
current_data['organisms list'].append(data)
file_path = "historic/{0}.pickle".format(self.parent_ecosystem.time)
with open(file_path, 'wb') as f:
pickle.dump(current_data, f)