def insert_into_pipeline(pipeline, transform, before=None, after=None):
 assert (before or after)
 cls = (before or after)
 for (i, t) in enumerate(pipeline):
  if isinstance(t, cls):
   break
 if after:
  i += 1
 return ((pipeline[:i] + [transform]) + pipeline[i:])

def insert_into_pipeline ( pipeline , transform , before = None , after = None ) : assert ( before or after ) cls = ( before or after ) for ( i , t ) in enumerate ( pipeline ) : if isinstance ( t , cls ) : break if after : i += 1 return ( ( pipeline [ : i ] + [ transform ] ) + pipeline [ i : ] )
insert a new transform into the pipeline after or before an instance of the given class .