Re: Bash-like pipes in Python

2016-03-18 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 something which is hard to read:
>>> from math import ceil, sqrt
>>> sqrt(pow(int(ceil(float("2.1"))), 2))
3.0
into something that is easy to read:
>>> pipe("2.1", float, ceil, int, lambda x: x*x, sqrt)
3.0

>>> pipe("atababsatsatsastatbadstssdhhhnbb", set, (sorted, {"reverse": True}))
['t', 's', 'n', 'h', 'd', 'b', 'a']



Cheers,
 Stefan


[0] https://github.com/sotte/pelpe
[1] 
http://elixir-lang.org/getting-started/enumerables-and-streams.html#the-pipe-operator
[2] https://github.com/sotte/pelper/blob/master/pelper/pelper.py#L14
Beste Grüße,
 Stefan



On Wed, Mar 16, 2016 at 4:29 PM, Random832  wrote:
> 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 (and classes? map and filter seem to be classes.) define
> __ror__ as calling the left-hand operand. Then this could be written as
> "abcd12345xyz" | pfilter(str.isdigit) | pmap(int) | preduce(mul).
>
> You could also define, say, __mul__ as partial application, so you could
> write "abcd12345xyz" | filter*str.isdigit | map*int | reduce*mul.
>
>> pfilter = partial(partial, filter)
>> pmap = partial(partial, map)
>> preduce = partial(partial, reduce)
>>
>> fpipe("abcd12345xyz", pfilter(str.isdigit), pmap(int), preduce(mul))
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


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 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  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
-- 
https://mail.python.org/mailman/listinfo/python-list


Python counterpart of Elixir's build tool Mix - or: why is it so cumbersome to "create packages"

2015-03-27 Thread Stefan Otte
Hey,

lately I was playing with elixir [1] and I found Mix [2], elixir's
"build/task tool", to be amazing. Creating new projects, compiling,
installing dependencies, running and testing -- really easy! For what
I'm concerned mix convinced me just for the "mix new projectname"
command:

* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/kv.ex
* creating test
* creating test/test_helper.exs
* creating test/kv_test.exs

It creates a reasonable skeleton which is the base for all the other
mix commands.
Yes, yes, there are skeleton generators for python, lots and lots of
blog posts that describe the ideal project layout, setup.py for
installing and testing, also lots and lots of blog posts about this...
You can do all of the tasks with python. Mix just seems to be more
"coherent".

Sorry, for the little rant! Mix just made me reevaluate my python
workflow again and I think there is room for improvement [4]. So what
is you take on it? What is your workflow? Do you have any tips?


Best,
 Stefan


[1] http://elixir-lang.org/
[2] http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html
[3] http://elixir-lang.org/docs/v1.0/mix/
[4] I'm not really sorry for the rant. Getting a fresh perspective, a
different point of view, is probably the main reason why one picks up
new languagesand it worked this time :)
-- 
https://mail.python.org/mailman/listinfo/python-list