#NEWLINE# def tail_avg( timeseries ) : #NEWLINE# #INDENT# '\n    This is a utility function used to calculate the average of the last three\n    datapoints in the series as a measure, instead of just the last datapoint.\n    It reduces noise, but it also reduces sensitivity and increases the delay\n    to detection.\n    ' #NEWLINE# try : #NEWLINE# #INDENT# t = ( ( ( timeseries [ ( - 1 ) ] [ 1 ] + timeseries [ ( - 2 ) ] [ 1 ] ) + timeseries [ ( - 3 ) ] [ 1 ] ) / 3 ) #NEWLINE# return t #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# return timeseries [ ( - 1 ) ] [ 1 ]