piper.text.pipe_parser

piper.text.pipe_parser(text: str, pipe_symbol: Optional[str] = None, info: bool = False, debug: bool = False)Tuple[str, Callable][source]

piper parser - split by pipe_symbol chained expression statements

Return completed pipeline of functions/commands, plus a boolean to determine whether the statement should be executed or not by the piper magic method (run_cmd).

Examples

%%piper --info
get_sample_sales() >>
select('-target_profit') >>
reset_index(drop=True) >>
head()

is rendered by the parser as:

(get_sample_sales()
.pipe(select, '-target_profit')
.pipe(reset_index, drop=True)
.pipe(head))

Expression assignment The parser also understands assignment in either R form (i.e. using <-) or Python (using ‘=’).

df <- pd.read_csv('inputs/test.csv')
df =  pd.read_csv('inputs/test.csv')

Note

There are two other ‘switches’ that can parse alternative linking symbols, namely –r and –dot

%%piper --r
sample_sales() %>%
group_by('location') %>%
summarise(sales=('actual_sales', 'sum')) %>%
assign(abc = lambda x: x.sales * 15.43,
       ratio = lambda x: ratio(x.sales, x.abc)) %>%
relocate(['abc', 'ratio'], 'before', 'sales') %>%
head(tablefmt='plain')

location         abc    ratio    sales
London      26210531        0  1698673
Milan       31162265        0  2019589
Paris       25582068        0  1657943
%%piper --dot
sample_sales()
.. group_by('location')
.. summarise(sales=('actual_sales', 'sum'))
.. assign(abc = lambda x: x.sales * 15.43,
          ratio = lambda x: ratio(x.sales, x.abc))
.. relocate(['abc', 'ratio'], 'before', 'sales')
.. head(tablefmt='plain')
Parameters
  • text – text to be parsed

  • pipe_symbol – default ‘>>’ pipe character used to uniquely identify to the piper parse when to replace it with the .pipe() statement.

  • info – default False. If True - print rendered linked ‘pipe’ statement

Returns

  • revised_stmt – revised ‘piped’ pandas pipeline statement

  • execute – ‘exec/eval’ - for magic run_cmd to determine whether the python statement returned should executed or evaluated. if revised_stmt is ‘assigned’ to another variable then ‘exec’ is used otherwise ‘eval’