-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLog_state.py
123 lines (82 loc) · 3.45 KB
/
Log_state.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
"""
This file is part of HDCS.
HDCS 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.
HDCS 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 HDCS. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import types
from time import time
from pprint import pprint
from Definitions import *
ln='\n'
def def_logging(matrix,defined,notes):
""" Log all aspects of the test definition, for later retrieval and
association with test data. Saves as a pure python file
"""
logno=0
while (os.path.isfile('./logs/'+"log_"+str(logno)+".csv")):
logno+=1
with open('./logs/'+"def_"+str(logno)+".py", "w") as log_file:
# Replace regulator with function name, not pointer, if necessary:
if callable(defined['R_pos']):
defined['R_pos'] = defined['R_pos'].__name__
log_file.write( """ # Test Definition for Log File """+str(logno) )
log_file.write('''\nTestNotes =""" ''')
pprint( (notes) , stream = log_file )
log_file.write(''' """\n''')
log_file.write("""TestParams = """)
pprint( (defined) , stream = log_file )
log_file.write( (ln+"""TestMatrix = """) )
pprint( (matrix) , stream = log_file )
regScript = open('RegulatorScript.py','r')
log_file.write( (regScript.read()) )
class Log(object):
def __init__(self,data_dicts):
""" This class operates extremely fast data logging to disk.
- To be extremely fast, we hold the file pointer until we
are completely done, at which point we close.
- We also make use of join method rather than concatenate, and
only perform 1 write per commit
"""
self.data_dicts = data_dicts # take a list of pointers to data dicts
self.active = 0
def begin(self):
self.start = time()
self.active = 1
logno=0
while (os.path.isfile('./logs/'+"log_"+str(logno)+".csv")):
logno+=1
self.file = open('./logs/'+"log_"+str(logno)+".csv",'a')
join_list = ['T'] # Time always goes first!
for data in self.data_dicts: # Get keys in each data_dict
join_list.extend([',%s' % key for key in sorted(data)])
join_list.extend(['\n'])
self.file.write(''.join(join_list))
def Commit(self,data_dicts):
""" Commit the current data dictionaries to file """
join_list = [str(time()-self.start)] # Time always goes first!
for data in data_dicts: # Get data in each data_dict
join_list.extend([',%s' % data[key] for key in sorted(data)])
join_list.extend(['\n'])
self.file.write(''.join(join_list))
def stop(self):
""" Close the log file """
self.file.close()
self.active = 0
if __name__ == '__main__':
dict1 = {'A':1,'B':2,'C':3.5}
dict2 = {'D':4,'E':5,'F':1241.00}
dictionaries = [dict1,dict2]
log = Log(dictionaries)
start =time()
log.Commit()
print(time()-start)
log.close()