Re: Bash-like pipes in Python

2016-03-20 Thread Marko Rauhamaa
Steven D'Aprano : > On Thu, 17 Mar 2016 02:20 am, Random832 wrote: >> fpipe("abcd12345xyz", pfilter(str.isdigit), pmap(int), preduce(mul)) > > Intriguing! Thank you for the suggestion. Still want the pipeline syntax! Marko --

Re: Bash-like pipes in Python

2016-03-20 Thread Random832
On Wed, Mar 16, 2016, at 11:20, Random832 wrote: > How about: > > from functools import partial, reduce > from operator import mul > def rcall(arg, func): return func(arg) > def fpipe(*args): return reduce(rcall, args) It occurs to me that this suggests a further refinement: have all functions

Re: Bash-like pipes in Python

2016-03-19 Thread Sivan Greenberg
If I understand correctly, the binary right or overloading that's seen here can be applied to any other computational objects. I could also think of implementing it for input / output pipes overloading the __ror__ method with .communicate() method of the Popen object [0]. -Sivan [0]:

Re: Bash-like pipes in Python

2016-03-19 Thread Marko Rauhamaa
Sivan Greenberg : > If I understand correctly, the binary right or overloading that's seen > here can be applied to any other computational objects. > > I could also think of implementing it for input / output pipes > overloading the __ror__ method with .communicate() method of

Re: Bash-like pipes in Python

2016-03-19 Thread Omar Abou Mrad
On Wed, Mar 16, 2016 at 4:57 PM, Steven D'Aprano wrote: > There's a powerful technique used in shell-scripting languages like bash: > pipes. The output of one function is piped in to become the input to the > next function. > > According to Martin Fowler, this was also used

Re: Bash-like pipes in Python

2016-03-19 Thread Stefan Otte
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

Re: Bash-like pipes in Python

2016-03-19 Thread Chris Angelico
On Thu, Mar 17, 2016 at 4:04 AM, Marko Rauhamaa wrote: > Question: Could the generators define __repr__ so you wouldn't need to > terminate the pipeline with "List" in interactive use? No no no no. You do NOT want __repr__ to fundamentally change the state of the object (which

Re: Bash-like pipes in Python

2016-03-19 Thread Steven D'Aprano
On Thu, 17 Mar 2016 04:04 am, Marko Rauhamaa wrote: > Steven D'Aprano : > >> Here is a way to do functional-programming-like pipelines to collect >> and transform values from an iterable: >> >> https://code.activestate.com/recipes/580625-collection-pipeline-in-python/ > >

Re: Bash-like pipes in Python

2016-03-19 Thread Random832
On Wed, Mar 16, 2016, at 10:57, Steven D'Aprano wrote: > For instance, we can take a string, extract all the digits, convert them > to > ints, and finally multiply the digits to give a final result: > > py> from operator import mul > py> "abcd12345xyz" | Filter(str.isdigit) | Map(int) |

Re: Bash-like pipes in Python

2016-03-19 Thread Marko Rauhamaa
Steven D'Aprano : > Here is a way to do functional-programming-like pipelines to collect > and transform values from an iterable: > > https://code.activestate.com/recipes/580625-collection-pipeline-in-python/ Nice. The other day we talked about Python replacing bash.

Re: Bash-like pipes in Python

2016-03-19 Thread Sivan Greenberg
++1 ! On Thu, Mar 17, 2016 at 6:31 PM, Random832 wrote: > > > On Thu, Mar 17, 2016, at 10:36, Chris Angelico wrote: > > This object has a generator/list duality, but if you observe it, it > > collapses to a list. When used interactively, it'd be pretty much the > > same

Re: Bash-like pipes in Python

2016-03-19 Thread Christian Gollwitzer
Am 16.03.16 um 16:09 schrieb Joel Goldstick: On Wed, Mar 16, 2016 at 10:57 AM, Steven D'Aprano wrote: py> from operator import mul py> "abcd12345xyz" | Filter(str.isdigit) | Map(int) | Reduce(mul) 120 This is interesting, but the part I'm missing is the use of the Pipe

Re: Bash-like pipes in Python

2016-03-19 Thread Steven D'Aprano
On Thu, 17 Mar 2016 02:20 am, Random832 wrote: > How about: > > from functools import partial, reduce > from operator import mul > def rcall(arg, func): return func(arg) > def fpipe(*args): return reduce(rcall, args) > pfilter = partial(partial, filter) > pmap = partial(partial, map) > preduce =

Re: Bash-like pipes in Python

2016-03-19 Thread Random832
On Wed, Mar 16, 2016, at 11:09, Joel Goldstick wrote: > > This is interesting, but the part I'm missing is the use of the Pipe > symbol '|' in python. Can you elaborate His "Filter", "Map", and "Reduce" are classes which define __ror__ methods, obviously. --

Re: Bash-like pipes in Python

2016-03-19 Thread Random832
On Thu, Mar 17, 2016, at 10:36, Chris Angelico wrote: > This object has a generator/list duality, but if you observe it, it > collapses to a list. When used interactively, it'd be pretty much the > same as calling list() as the last step, but in a script, they'd > operate lazily. > > Quantum

Bash-like pipes in Python

2016-03-19 Thread Steven D'Aprano
There's a powerful technique used in shell-scripting languages like bash: pipes. The output of one function is piped in to become the input to the next function. According to Martin Fowler, this was also used extensively in Smalltalk: http://martinfowler.com/articles/collection-pipeline/ and

Re: Bash-like pipes in Python

2016-03-19 Thread Omar Abou Mrad
On Wed, Mar 16, 2016 at 5:39 PM, Steven D'Aprano 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

Re: Bash-like pipes in Python

2016-03-19 Thread Stefan Otte
I wrote this little lib "pelper" [0] which has the elixir inspired pipe [1]. I initially had an implementation that used operator overloading but found that the "|" syntax was not really necessary. I just use the function `pipe` [2] Some examples from the repo: ``pipe`` allows you to turn

Re: Bash-like pipes in Python

2016-03-19 Thread Steven D'Aprano
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

Re: Bash-like pipes in Python

2016-03-18 Thread Joel Goldstick
On Wed, Mar 16, 2016 at 10:57 AM, Steven D'Aprano wrote: > There's a powerful technique used in shell-scripting languages like bash: > pipes. The output of one function is piped in to become the input to the > next function. > > According to Martin Fowler, this was also used

Re: Bash-like pipes in Python

2016-03-18 Thread Sven R. Kunze
On 16.03.2016 16:09, Joel Goldstick wrote: symbol '|' in python. Can you elaborate bitwise or -- https://mail.python.org/mailman/listinfo/python-list

Re: Bash-like pipes in Python

2016-03-18 Thread Chris Angelico
On Fri, Mar 18, 2016 at 1:10 AM, Steven D'Aprano wrote: > At the moment, the data being processed by the Map, Filter, etc. are > ordinary lists or iterators. In order to give them a customer __repr__, I > would have to change the Map and Filter __ror__ method to return some >