-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
36 lines (30 loc) · 991 Bytes
/
data.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
"""Handle data operations like writing to a file.
"""
from walk import Walk
class Data():
"""Wrapper to output data.
"""
def __init__(self, walks: list[Walk]):
self._walks = walks
self._fname = 'rrwg.dat'
self._dataf = open(self._fname, 'w')
for i, walk in enumerate(walks):
for j in walk.vertices():
self._dataf.write('\tw{}v{}'.format(i, j))
self._dataf.write('\n')
self.write()
def __del__(self):
if self._dataf:
self._dataf.close()
print('* Wrote {}'.format(self._fname))
def write(self):
"""Write the current data saved in walk list
to the output file.
"""
for walk in self._walks:
allvis = walk.total_visits()
for i in walk.vertices():
vis = walk.nvisits(i)/allvis
self._dataf.write('\t{:.3f}'.format(vis))
self._dataf.write('\n')
self._dataf.flush()