Bash-like pipes in Python

Stefan Otte stefan.otte at gmail.com
Wed Mar 16 12:03:50 EDT 2016


I use the pipe style a lot, but one of the annoyances is that many
functions in the python standardlib expect the first argument to be a
callable and the second an iterable. I tend to write a lot of
"p-functions" (functions which switch that order and make them
compatible to `pipe`).


from pelper import pipe
from operator import mul

def pfilter(iterable, f): return filter(f, iterable)
def pmap(iterable, f): return map(f, iterable)
def preduce(iterable, f): return reduce(f, iterable)

pipe("abcd12345xyz",
     (pfilter, str.isdigit),
     (pmap, int),
     (preduce, mul)
)

`pipe` also allows you to us named arguments which would be difficult
if you use operator overloading:
pipe("sentaoisrntuwyo", (sorted, {"reverse": True}))


Beste Grüße,
 Stefan



On Wed, Mar 16, 2016 at 4:39 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Thu, 17 Mar 2016 02:22 am, Omar Abou Mrad wrote:
>
>> Would be nice if this was possible:
>>
>>>>> get_digits = Filter(str.isdigit) | Map(int)
>>>>> 'kjkjsdf399834' | get_digits
>
>
> Yes it would. I'll work on that.
>
>
>> Also, how about using '>>' instead of '|' for "Forward chaining"
>
> Any particular reason you prefer >> over | as the operator?
>
>
>
>
> --
> Steven
>
> --
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list