It is often common to write this type of python code:
def load_data():
df_ans = get_data()
df_checked = check_data(df_ans) # returns df_ans or raises an error
return_dict = format_data(df_ans)
return return_dict
One could write the above like this (but it's ugly in my opinion)
def load_data():
return format_data(check_data(get_data()))
If this were all pandas code, one could use the .pipe method to do as follows:
def load_data():
return get_data().pipe(check_data).pipe(format_data)
However, there seems to be no universal way to "pipe" things in Python, something like:
def load_data():
return (get_data() |> check_data |> format_data)
Is there any PEP proposal for this? I could not find one.
I've seen some libraries that do operator overloading on |
but I don't think that's what I'm looking for - I'm looking for x {operator} y
to be the same as y(x)
(which of course requires y
to be callable)
I don't think a pipe operator in Python will be approved in the near future. To do that operation there is already the function pipe
in the toolz
library.
It is not a Python built in library but is widely used.
In case you are curious the implementation is just:
def pipe(data, *funcs):
for func in funcs:
data = func(data)
return data