def apply_to_window(sequence, window_size, function, step=None):
 seqlen = len(sequence)
 if (step is None):
  step = max((window_size // 2), 1)
 else:
  step = max(step, 1)
 results = []
 pos = 0
 while (pos < ((seqlen - window_size) + 1)):
  (start, middle, end) = (pos, (((pos + window_size) + pos) // 2), (pos + window_size))
  fragment = sequence[start:end]
  value = function(fragment)
  results.append((middle, value))
  pos += step
 if (pos != (seqlen - window_size)):
  pos = (seqlen - window_size)
  (start, middle, end) = (pos, (((pos + window_size) + pos) // 2), (pos + window_size))
  fragment = sequence[start:end]
  value = function(fragment)
  results.append((middle, value))
 return results

def apply_to_window ( sequence , window_size , function , step = None ) : seqlen = len ( sequence ) if ( step is None ) : step = max ( ( window_size // 2 ) , 1 ) else : step = max ( step , 1 ) results = [ ] pos = 0 while ( pos < ( ( seqlen - window_size ) + 1 ) ) : ( start , middle , end ) = ( pos , ( ( ( pos + window_size ) + pos ) // 2 ) , ( pos + window_size ) ) fragment = sequence [ start : end ] value = function ( fragment ) results . append ( ( middle , value ) ) pos += step if ( pos != ( seqlen - window_size ) ) : pos = ( seqlen - window_size ) ( start , middle , end ) = ( pos , ( ( ( pos + window_size ) + pos ) // 2 ) , ( pos + window_size ) ) fragment = sequence [ start : end ] value = function ( fragment ) results . append ( ( middle , value ) ) return results
returns a list of tuples for fragments of the passed sequence of length window_size .