Pipe: Infix syntax for Python
dev-tricks.net
dev-tricks.net
the problem is, I rarely feel like I can just use all these cool-syntax libraries for production, since they don't add a lot of value. why do I need a whole dependency dependency for something that just make one or two line more readable?
I've written a syntax enhancer for PHP once, but it just added one feature (array dereferencing) for a small but noticeable price of setting up; I never considered it for production usage because there weren't enough features to justify it. (CoffeeScript is an example of justifiable price for huge changes and multiple ideas)
for personal just-for-fun projects (like solving project euler problems) it's an awesome idea, though
This may enable a completely different and cleaner architecture or a mess. Probably as always it starts with a mess and results in clean along the way once you resolve all the problems you should know about but didn't.
Key point is that this is exactly the way our languages and industry evolves. jQuery also started as someones just-for-fun project and poised risk (and still does) to all the projects that used it.
In reality a person needs to go and just use stuff like this, with experience you learn to see if something works or not.
Then I could write my code in whatever syntax I want, and someone else could flip a switch and change it into their preferred style of syntax.
I am honestly kind of puzzled as to why the concept of MVC disappears as soon as we start writing coding tools. It seems that for any reasonable syntax that you can write a parser for; it should also be possible to write an emitter from an 'intermediate' syntax tree.
But more than once-as simple as putting null before equalities in conditionals, or as complex as setting up squid to cache just robots.txt for a crawler-I've been surprised and glad that I'm working with that quirky dude who insists on paying a small price for something useful. And I find myself using it again in the future because it actually gave me real value, made me and my team more productive, or happier, and hopefully made our product better.
[1]: http://docs.python.org/reference/expressions.html#summary
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
Anyway, the order of the pipeline-like operation in Python sometimes really bugs me. This one addresses part of it.Recently I'm playing with Scala and it is really neat to do these stuff:
// A lazy Fibonacci generator (a Scala Stream)
lazy val fib: Stream[Long] = Stream.cons(0, Stream.cons(1,
fib.zip(fib.tail).map(pair => pair._1 + pair._2)))
fib filter(_ % 2 == 0) takeWhile (_ < 4000000) sum
// 4613732http://stackoverflow.com/questions/4961017/clojure-style-fun...
Some answers seem enlightening:
While I sympathize with the desire to create cool new language constructs (à la Lisp macros), it is not really the Python philosophy to do this:
>>> import this
[...]
There should be one-- and preferably only one --obvious way to do it. bash$ osh gen 10 ^ f 'x: x**2' ^ red + $
^ is the pipe symbol, red means "reduce", $ means output, and f means apply the given function.The same capabilities are available as a Python library:
from osh.api import *
osh(gen(10), f(lambda x: x**2), red(lambda x, y: x + y), out())
or from osh.api import *
osh(gen(10), f(lambda x: x**2), red('+'), out())
Osh can also run results on multiple hosts at once, in parallel, combining the results in various ways; and integrates database access, piping tuples to and from sql commands.For instance:
[1,2,3,4,5] | p(sum)
where p would transform a def(seq, args, kwargs) to def(args,kwargs) and yield the results?Also, maybe:
[1,2,3,4,5] >> sum
where that could be made automatically ? @Pipe
def p(iterable, function, **kwargs):
return function(iterable, **kwargs)
In [3]: from pipe import *
In [4]: [1,2,3,4,5] | p(sum)
Out[4]: 15 >>> from pipe import *
>>> [1, 2, 3, 4, 5] | Pipe(sum)
15On standard US keyboards, it's shift-backslash. YLMV.
The first example could have been written like this, which is much simpler and easier to read than both the prefix and infix versions:
sum(x * x for x in take_while(fib(), lambda x: x < 1000000) if x % 2)
http://stackoverflow.com/questions/2062540/help-with-linq-ex...
It’s all fully tested (python setup.py test), and I really like using it, but I haven't done so in any large-ish production app yet because it just looks completely different to the surrounding code. I have used it with success in some small scripts, but that's about it.
I try to use ipython and pipe as my shell to see how it feels.