[a18f15]: / utilities / logUtils.py

Download this file

32 lines (25 with data), 806 Bytes

 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
import json
import csv
def LOG2CSV(datalist, csv_file, flag = 'a'):
'''
datalist: List of elements to be written
'''
with open(csv_file, flag) as csvFile:
writer = csv.writer(csvFile)
writer.writerow(datalist)
csvFile.close()
def LOG2TXT(text, file_path, flag = 'a', console= True):
'''
text: python object with stats to be logged
'''
text = str(text)
with open(file_path, 'a', buffering=1) as txt_file:
if console: print(text)
print(text, file=txt_file)
def LOG2DICTXT(dic, file_path, flag = 'a', console= True):
'''
stats: dictionary object with stats to be logged
'''
with open(file_path, 'a', buffering=1) as txt_file:
if console: print(json.dumps(dic))
print(json.dumps(dic), file=txt_file)