from datetime import datetime, timedelta
frames_load = 0
times_start_dict = [{'timestamp': datetime(2013, 12, 21, 4, 36, 23)}, {'timestamp': datetime(2013, 12, 21, 4, 36, 23)}]
times_end_dict = [{'timestamp': datetime(2013, 12, 21, 4, 36, 53)}, {'timestamp': datetime(2013, 12, 21, 4, 36, 53)}]
for start in times_start_dict:
for end in times_end_dict:
if check_startend(): #dummy function to check other value in dict to see if start/end times match; please comment this out and indent code block to reproduce error
frames_load += end['timestamp'] - start['timestamp']
print frames_load
continue
Output:
Traceback (line 11): "frames_load += end['timestamp'] - start['timestamp']"
TypeError: unsupported operand type(s) for +=: 'int' and 'datetime.timedelta'
Some context:
I'm iterating over two lists consisting of dicts which contain datetime objects for start and end times, respectively. Once a start and end timedelta have been correctly matched (not shown), I find the time elapsed which becomes a timedelta object. I'm assigning it to frames_load so that in the next iteration, I can add the time elapsed (which should always be positive), figuring it's just like incrementing x += 1. However, I'm getting a TypeError which says to me that after the first iteration/calculation, frames_load is now an int and no longer a timedelta object.
How can I add timedeltas to frames_load so that it can display the aggregate/sum timedelta?
int(...).frames_load += end['timestamp'] - start['timestamp']. The same line in the error traceback readsframes_load += int(end['timestamp'] - start['timestamp']). So the code is not the same code that produced the error. This makes it hard to give a precise answer. Also you should show minimal code initializingframes_loadand besttimes_start_dictandtimes_end_dictas well, so everyone can reproduce the error.frames_load = 0useframes_load = timedelta().