modules/tools/DataLog: separate name and timestamp
@@ -8,20 +8,20 @@ from utime import localtime, ticks_us
class DataLog():
- def __init__(self, *headers, path=None, ext='txt'):
+ def __init__(self, *headers, name='log', timestamp=True, ext='csv'):
- # If no path is given, it will be ./log_yyyy_mm_dd_hh_mm_ss_uuuuuu
- if path is None:
+ # Make timestamp of the form yyyy_mm_dd_hh_mm_ss_uuuuuu
+ if timestamp:
y, mo, d, h, mi, s = localtime()[0:6]
u = ticks_us() % 1000000
- prefix = 'log_{0}_{1:02d}_{2:02d}_{3:02d}_{4:02d}_{5:02d}_{6:06d}'.format(y, mo, d, h, mi, s, u)
+ stamp = '_{0}_{1:02d}_{2:02d}_{3:02d}_{4:02d}_{5:02d}_{6:06d}'.format(y, mo, d, h, mi, s, u)
else:
- prefix = path
+ stamp = ''
# Append extension and open
- self.file = open('{0}.{1}'.format(prefix, ext), 'w+')
+ self.file = open('{0}{1}.{2}'.format(name, stamp, ext), 'w+')
- # Of column headers were given, print those as first line
+ # If column headers were given, print those as first line
if len(headers) > 0:
print(*headers, sep=', ', file=self.file)
