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
If you don't mind decorating all functions where pipes are used with @pipes
, you may like robinhilliard/pipes.
It is inspired by the Elixir syntax:
@pipes
def pretty_pipe():
print (
range(-5, 0)
<< map(lambda x: x + 1)
<< map(abs)
<< map(str)
>> tuple
) # prints ('4', '3', '2', '1', '0')
The >>
works similarly to |>
in Elixir, while <<
is implemented to circumvent APIs like map
and reduce
where the right parameter is the iterables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With