#NEWLINE# def mutate( table , exprs = None , **kwds ) : #NEWLINE# #INDENT# '\n    Convenience function for table projections involving adding columns\n\n    Parameters\n    ----------\n    exprs : list, default None\n      List of named expressions to add as columns\n    kwds : keywords for new columns\n\n    Examples\n    --------\n    expr = table.mutate(qux=table.foo + table.bar, baz=5)\n\n    Returns\n    -------\n    mutated : TableExpr\n    ' #NEWLINE# if ( exprs is None ) : #NEWLINE# #INDENT# exprs = [ ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# exprs = util . promote_list ( exprs ) #NEWLINE# #UNINDENT# for ( k , v ) in sorted ( kwds . items ( ) ) : #NEWLINE# #INDENT# if util . is_function ( v ) : #NEWLINE# #INDENT# v = v ( table ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# v = as_value_expr ( v ) #NEWLINE# #UNINDENT# exprs . append ( v . name ( k ) ) #NEWLINE# #UNINDENT# has_replacement = False #NEWLINE# for expr in exprs : #NEWLINE# #INDENT# if ( expr . get_name ( ) in table ) : #NEWLINE# #INDENT# has_replacement = True #NEWLINE# #UNINDENT# #UNINDENT# if v : #NEWLINE# #INDENT# by_name = dict ( ( ( x . get_name ( ) , x ) for x in exprs ) ) #NEWLINE# used = set ( ) #NEWLINE# proj_exprs = [ ] #NEWLINE# for c in table . columns : #NEWLINE# #INDENT# if ( c in by_name ) : #NEWLINE# #INDENT# proj_exprs . append ( by_name [ c ] ) #NEWLINE# used . add ( c ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# proj_exprs . append ( c ) #NEWLINE# #UNINDENT# #UNINDENT# for x in exprs : #NEWLINE# #INDENT# if ( x . get_name ( ) not in used ) : #NEWLINE# #INDENT# proj_exprs . append ( x ) #NEWLINE# #UNINDENT# #UNINDENT# return table . projection ( proj_exprs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return table . projection ( ( [ table ] + exprs ) )