-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathin_out_module.py
More file actions
151 lines (127 loc) · 5.01 KB
/
in_out_module.py
File metadata and controls
151 lines (127 loc) · 5.01 KB
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
141
142
143
144
145
146
147
148
149
150
151
"""This module will contain functions for reading from file and
for writing to file"""
from shutil import copyfile, move
import datetime
def write_data(list_cells, time):
""" Function add line in data.txt file
Parameters
----------
list_cells : list
list of cells
time : int
current time = step of modelling
Returns
----------
"""
list_victim = [cell for cell in list_cells if not cell.predator]
list_predator = [cell for cell in list_cells if cell.predator]
list_victim_age = [cell.age for cell in list_victim]
list_predator_age = [cell.age for cell in list_predator]
list_victim_engine = [cell.engines for cell in list_victim]
list_predator_engine = [cell.engines for cell in list_predator]
list_victim_satiety = [cell.satiety * 100 for cell in list_victim]
list_predator_satiety = [cell.satiety * 100 for cell in list_predator]
number_of_victims = len(list_victim)
number_of_predators = len(list_predator)
victim_average_age = (sum(list_victim_age) / number_of_victims
if number_of_victims > 0 else 0)
predator_average_age = (sum(list_predator_age) / number_of_predators
if number_of_predators > 0 else 0)
victim_average_engines = (sum(list_victim_engine) / number_of_victims
if number_of_victims > 0 else 0)
predator_average_engines = (sum(list_predator_engine) / number_of_predators
if number_of_predators > 0 else 0)
victim_average_satiety = (sum(list_victim_satiety) / number_of_victims
if number_of_victims > 0 else 0)
predator_average_satiety = (sum(list_predator_satiety) / number_of_predators
if number_of_predators > 0 else 0)
time_in_file = read_data('data.txt')[0]
last_time_in_file = 0
if len(time_in_file) > 0:
last_time_in_file = max(time_in_file)
if number_of_victims > 0 or number_of_predators > 0 or time > last_time_in_file:
with open('data.txt', 'a') as file:
cell_data = [str(time),
str(number_of_victims),
str(number_of_predators),
str(victim_average_age),
str(predator_average_age),
str(victim_average_engines),
str(predator_average_engines),
str(victim_average_satiety),
str(predator_average_satiety),
]
cell_data = ' '.join(cell_data)
cell_data = cell_data + '\n'
file.write(cell_data)
def read_data(file_name):
""" Function reads data from 'file_name' file
Parameters
----------
file_name : string
name of the data file
Returns
----------
victims_list : list
list of the victim's population at the particular moment in time
predators_list : list
list of the predator's population at the particular moment in time
time : list
list of the particular moments
"""
time_list = []
input_data = []
victims_list = []
predators_list = []
victims_list_mid_age = []
predators_list_mid_age = []
victims_list_mid_engine = []
predators_list_mid_engine = []
victims_list_mid_satiety = []
predators_list_mid_satiety = []
with open(file_name, 'r') as file:
for line in file:
input_data.append(line.split())
for i in range(1, len(input_data)):
time_list.append(int(input_data[i][0]))
victims_list.append(int(input_data[i][1]))
predators_list.append(int(input_data[i][2]))
victims_list_mid_age.append(float(input_data[i][3]))
predators_list_mid_age.append(float(input_data[i][4]))
victims_list_mid_engine.append(float(input_data[i][5]))
predators_list_mid_engine.append(float(input_data[i][6]))
victims_list_mid_satiety.append(float(input_data[i][7]))
predators_list_mid_satiety.append(float(input_data[i][8]))
return (time_list,
victims_list, predators_list,
victims_list_mid_age, predators_list_mid_age,
victims_list_mid_engine, predators_list_mid_engine,
victims_list_mid_satiety, predators_list_mid_satiety,
)
def save_file(file_name, folder_name):
""" Function copies 'data.txt' file to 'data_yyyy_mm_dd_hh_mm_ss.txt' file
Parameters
----------
file_name : string
name of the data file
folder_name : string
name of the data file folder
Returns
----------
"""
x = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
a = 'data_' + str(x) + '.txt'
copyfile(file_name, str(a))
move(str(a), folder_name)
clean_file(file_name)
def clean_file(file_name):
""" Function cleans 'data.txt' file
Parameters
----------
file_name : string
name of the data file
Returns
----------
"""
file = open(file_name, 'w')
file.close()