我正在做一个日期时间验证项目。具体来说,请确保每个时间间隔都有一个日期时间。如果间隔中缺少日期时间,则将缺少的值添加到Timestamp对象的列表中。
当我运行以下代码时,即使大多数日期时间都没有,也将其标记为Missing。我相信这是一个索引问题。我要去哪里错了?
代码:
import datetime
_RRATE_ = [0,1,0] #data refresh rate in [minute, hour, day]
LOCALE = "Seattle" #location of cell site
timesList = [] #initialize list of Timestamps with Errors filled in
#Class Timestamp allows us to store all data for a single timestamp in one object.
class Timestamp:
x = 1 #gives each new timestamp object a unique identifier for the dataset
missingStamps = 0
def __init__(self, datetime, location, errorMessage):
self.timestamp = datetime
self.location = location
self.error = errorMessage
self.UID = Timestamp.x
Timestamp.x+=1
timesList.append(self)
def updateError(self, newError):
self.error = newError
def __repr__(self): #make print timesList look neat
return str(self.UID) + " " + str(self.timestamp) + ", " + self.location + ", " + self.error
def duplicates(index):
tempList = []
for timestamp in timesList:
tempList.append(timestamp.timestamp)
if datetimes[index] in tempList:
Timestamp(datetimes[index],LOCALE, "duplicate")
return True
else:
return False
def outOfPlace(index):
for timestampObj in timesList:
if(timestampObj.timestamp > datetimes[index]):
Timestamp(datetimes[index],LOCALE, "out of place")
return True
else:
return False
def missingTimestamp(datetimeObj):
Timestamp.missingStamps = 0
if dateAdd(timesList[len(timesList)-1].timestamp) < datetimeObj:
while dateAdd(timesList[len(timesList)-1].timestamp) < datetimeObj:
Timestamp(dateAdd(timesList[len(timesList)-1].timestamp), LOCALE, "missing timestamp")
Timestamp.missingStamps+=1
return True
else:
return False
def dateAdd(datetimeObj):
return (datetimeObj + datetime.timedelta(minutes = _RRATE_[0], hours = _RRATE_[1], days = _RRATE_[2]))
def main():
Timestamp(datetimes[0],LOCALE, "no error")
errorCount = 0
for i in range(1,len(datetimes)):
if duplicates(i):
errorCount+=1
elif outOfPlace(i):
errorCount+=1
elif (missingTimestamp(datetimes[i]))>0:
errorCount+=Timestamp.missingStamps
Timestamp.missingStamps = 0
else:
Timestamp(datetimes[i],LOCALE, "no error")
for elem in timesList:
print elem
b = datetime.datetime(2018,7,20)
c = datetime.datetime(2018,7,20,1)
d = datetime.datetime(2018,7,20,2)
e = datetime.datetime(2018,7,20,7)
f = datetime.datetime(2018,7,20,7)
g = datetime.datetime(2018,7,20,9)
h = datetime.datetime(2018,7,20,16)
i = datetime.datetime(2018,7,20,16)
j = datetime.datetime(2018,7,20,9)
datetimes = [b,c,d,e,f,g,h,i,j]
main()
答案 0 :(得分:0)
如@Prune在对问题的评论中所指定的那样,您应该对问题进行更完整的描述。
话虽如此,我运行了您的代码,并从我得到的输出中,问题似乎出在主函数中的控制流语句的逻辑上,
elif (missingTimestamp(datetimes[i]))>0:
errorCount+=Timestamp.missingStamps
Timestamp.missingStamps = 0
遇到缺少的时间戳时,例如在d
和e
之间,missingTimestamp
函数最多填充e
的值,但不包括e
,并且控制块不会处理函数返回之后。然后它将移至f
,并且e
未放置在全局timesList
变量中。
一个简单的解决方案是将elif
块更改为:
elif (missingTimestamp(datetimes[i]))>0:
errorCount+=Timestamp.missingStamps
Timestamp.missingStamps = 0
# Line below to add current value to global timeList variable
Timestamp(datetimes[i],LOCALE, "no error")