-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_logger.py
47 lines (39 loc) · 1.42 KB
/
csv_logger.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
"""
Module to log sensors data during the operation.
A CSV file is created or appended to with energy generation, import, A/C
settings and sensors data every time a read is performed in the main loop.
"""
import csv
import logging
import os
LOGGER = logging.getLogger(__name__)
class CSVLogger:
"""
Simple data logger using a CSV file
Manually flush on save() to avoid too many commits on SD cards.
"""
def __init__(self, filename, varlist):
"""
Pass filename to create/append to, and a list of variable names
to be used for the header row.
"""
self.variables = varlist
if os.path.isfile(filename):
self.file = open(filename, 'a', newline='')
self.writer = csv.writer(self.file)
else:
self.file = open(filename, 'w', newline='')
self.writer = csv.writer(self.file)
self.writer.writerow(self.variables)
def __del__(self):
"""Close file on destruction"""
self.file.close()
def write(self, values_list):
"""Write a new line with data"""
if len(values_list) != len(self.variables):
raise ValueError('Invalid number of values in CSVLogger.write: expected %d, got %d' %
(len(self.variables), len(values_list)))
self.writer.writerow(values_list)
def save(self):
"""Save file to disk"""
self.file.flush()