-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc_file_tidy_utils.py
186 lines (140 loc) · 5.41 KB
/
cc_file_tidy_utils.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Code to make all cc_files coherent:
strip all non readings and split them up into day files
You probably only want to use createDayCCFiles
"""
import os, re, csv
import datetime as dt
# Variables that determine filenames:
DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
DATE_FORMAT = '%Y-%m-%d'
RAW_FILE_ENDING = '_cc.csv'
NEW_FILE_ENDING = 'clean_cc.csv'
def createDayCCFiles(srcFolder,destFolder):
"""
Main Function use it
"""
ccFiles = getCCFilesFromDir(srcFolder)
readings = getAllReadings(ccFiles)
os.chdir(destFolder)
writeReadingsToDayFiles(readings)
def isRawCCFileName(fName):
"""
predicate: True if fName arg is of format 'timestamp_cc.csv'
used as helper function to getCCFilesFromDir
>>> isRawCCFileName('2012-07-28T00:28:02_cc.csv')
True
>>> isRawCCFileName('Xcc_2012-07-28T12:12:12.csv')
False
>>> isRawCCFileName('awef')
False
"""
regExpr = r'[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}_cc.csv'
# could restrict re further according to valid date values
found = re.findall(regExpr,fName)
if len(fName) == 26 and len(found) == 1:
# IF test 1: string not larger than expected
# AND test 2: one (and only one) regexpr match found
return True
else:
return False
def getCCFilesFromDir(pathName):
"""
returns a sorted list of cc files from directory given as argument.
The timestamp in a ccfile name ensures that the list of files can be sorted.
"""
files = os.listdir(pathName)
files = [f for f in files if isRawCCFileName(f)]
files = [pathName + f for f in files] # add the path
files = sorted(files)
return files
def getFilesByDateStamp(dtStmp,fileList):
"""
returns a list of all files in cwd with datestamp in file name equal to argument
>>> files = ['CC_2012-07-28T00:26:34_cc.csv','CC_2013-02-27T05:45:12_cc.csv','CC_2012-07-27T00:26:34_cc.csv','CC_2012-07-28T23:52:21_cc.csv']
>>> getFilesByDateStamp('2012-07-28',files)
['CC_2012-07-28T00:26:34_cc.csv', 'CC_2012-07-28T23:52:21_cc.csv']
"""
files = []
for fileName in fileList:
fileDate = fileName[3:13]
if fileDate == dtStmp:
files.append(fileName)
return files
def isDateChanged(timeStr):
"""
returns boolean. Confirms if midnight has passed.
helper function for getAllReadings
"""
return re.findall(r'00:00:[0-9][0-9]',timeStr)
def getAllReadings(fileList):
"""
returns a list of all readings found
"""
dtype = [] # datatype will be populated with all [timestamp,watt] pairs
# create super list of all the readings:
for fName in fileList:
dtName = re.findall(r"[0-9]{4}-[0-9]{2}-[0-9]{2}",fName)[0]
currDate = dt.datetime.strptime(dtName,DATE_FORMAT) # curr date is file name
with open(fName,'r') as f:
nextDay = False # date changed only once
reader = csv.reader(f)
for line in reader: # reader rtrns ['time','watt'] per row
if isDateChanged(line[0][:8]) and not nextDay:
currDate = currDate + dt.timedelta(days=1) # increment currDate
nextDay = True
# this clause deals with old file recordings that didn't include a full datetime str
if len(line[0]) == 8 : # only apply on lines that don't have full datetime str
line[0] = currDate.isoformat()[:10] + 'T' + line[0]
if line[1].isdigit():
dtype.append(line)
return dtype
def writeReadingsToDayFiles(readings):
"""
writes all readings to disk in one day files
"""
# create new files for each value in date change
locDateChngs = getListOfDateChanges(readings)
locDateChngs.append(len(readings)) # to capture last reading
for nmbr in range(len(locDateChngs) - 1):
daySlice = readings[locDateChngs[nmbr]:locDateChngs[nmbr+1]] # slice corresponding to day
writeToDisk(daySlice)
def getListOfDateChanges(readings):
"""
returns a list of all the indexes where a date change occurs
(helper function for writeReadingsToDayFiles)
"""
# get list of all the date changes:
dateChngs = []
for i in range(len(readings)):
if not readings[i-1][0][:10] == readings[i][0][:10]: # first is 0 as last diff from 0
dateChngs.append(i)
return dateChngs
def writeToDisk(readings):
"""
writes a list to disk as csv file
(helper function to writeReadingsToDayFiles)
"""
# new fileNames is timestamp of 1st val + globally defined ending:
fName = readings[0][0][:10] + NEW_FILE_ENDING
with open(fName,'w') as f:
writer = csv.writer(f)
writer.writerows(readings)
def getFirstDate(dtStrFileList):
"""
returns datetime obj that represents first date found in a list of DATETIME_FORMAT strings
"""
dtStrFileList = sorted(dtStrFileList)
first_dtStr = dtStrFileList[0]
fDate = dt.datetime.strptime(first_dtStr,DATETIME_FORMAT + RAW_FILE_ENDING)
return fDate
def getLastDate(dtStrFileList):
"""
returns datetime obj that represents last date found in a list of DATETIME_FORMAT strings
"""
dtStrFileList = sorted(dtStrFileList)
last_dtStr = dtStrFileList[-1]
lDate = dt.datetime.strptime(last_dtStr,DATETIME_FORMAT + RAW_FILE_ENDING)
return lastDate
import doctest
doctest.testmod()