def _InsertNodeAt(new_node, target, after=False):
 if (new_node.parent is not None):
  raise RuntimeError('inserting node which already has a parent', (new_node, new_node.parent))
 parent_of_target = target.parent
 if (parent_of_target is None):
  raise RuntimeError('expected target node to have a parent', (target,))
 for (i, child) in enumerate(parent_of_target.children):
  if (child is target):
   insertion_index = ((i + 1) if after else i)
   parent_of_target.insert_child(insertion_index, new_node)
   return
 raise RuntimeError('unable to find insertion point for target node', (target,))

def _InsertNodeAt ( new_node , target , after = False ) : if ( new_node . parent is not None ) : raise RuntimeError ( 'inserting node which already has a parent' , ( new_node , new_node . parent ) ) parent_of_target = target . parent if ( parent_of_target is None ) : raise RuntimeError ( 'expected target node to have a parent' , ( target , ) ) for ( i , child ) in enumerate ( parent_of_target . children ) : if ( child is target ) : insertion_index = ( ( i + 1 ) if after else i ) parent_of_target . insert_child ( insertion_index , new_node ) return raise RuntimeError ( 'unable to find insertion point for target node' , ( target , ) )
underlying implementation for node insertion .