7 ms·
Pipe: Infix syntax for Python
- pepijndevos 16y agoServer is unresponsive. Maybe Coral will cache it sometime: http://dev-tricks.net.nyud.net/pipe-infix-syntax-for-python http://dev-tricks.net.nyud.net/pipe-infix-syntax-for-python
- deleted 16y ago[deleted]
- toni 16y agogithub repo: https://github.com/JulienPalard/Pipe/ https://github.com/JulienPalard/Pipe/
- ivank 16y agoPossibly inspired by Pypes: http://arvindn.livejournal.com/68137.html http://arvindn.livejournal.com/68137.html
- biot 16y agoThis reminds me of C#'s LINQ, but for Python... PLINQ? http://stackoverflow.com/questions/2062540/help-with-linq-expression/2062565#2062565 http://stackoverflow.com/questions/2062540/help-with-linq-ex...
- mmavnn 16y agoHe mentions in the link that linq is an inspiration.
- biot 16y agoThanks, I had missed that in the comments.
- darklajid 16y agoThis reminds me more of the |> 'operator' in F#, doing the ~very same thing.
- draegtun 16y agoAnd the feed operator in Perl6: http://perl6advent.wordpress.com/2010/12/10/day-10-feed-operators/ http://perl6advent.wordpress.com/2010/12/10/day-10-feed-oper...
- mmavnn 16y agoVery nice piece of sugar. Pipes are one of the things I love about f#, and it's nice to have a quick and easy implementation for when I'm playing with Django/App Engine.
- va1en0k 16y agothat's great! I always wanted something like this for Python 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
- JanezStupar 16y agoWell, the way I see it - these tools open up whole new array of possibilities of writing code in a more intuitive fashion. 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.
- djacobs 16y agoThe way I code Python, this has a real potential to clean up any codebase I'm working on in a major way. (I've complained several times about the need for intuitive chaining in Python.)
- ohyes 16y agoWhat I would like to see is a way to separate the the syntax of my code from the semantics of my code. Ideally, my code editor should be able to take the same piece of code and display it in a few different styles of syntax. 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.
- aufreak3 16y agoRemove the pipe symbols and you get Forth syntax!
- srean 16y agoCoincidentally enough I was looking at http://code.google.com/p/python-pipeline/ http://code.google.com/p/python-pipeline/ a few days ago. I fully expected them to be related by a fork, but they dont seem to be. Python-pipeline executes a similar idea, but to infix-chain iterators by way of generators. I guess some abstractions are just too handy and useful and the chances are high that they will be implemented/discovered independently. Pipe is just one of them.
- d0m 16y agoIs there any helper function that convert existing functions into pipe's style? 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 ?
- heyitsnick 16y agoKeep in mind I have no idea how this magical code works. In fact I can't even find the pipe character on my keyboard right now (I am copy and pasting it!). But this seems to work: @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
- yesbabyyes 16y agoOr even just wrap the function in Pipe: >>> from pipe import * >>> [1, 2, 3, 4, 5] | Pipe(sum) 15
- jerf 16y ago"In fact I can't even find the pipe character on my keyboard right now" On standard US keyboards, it's shift-backslash. YLMV.
- lloeki 16y agoOn Mac keyboards it's alt+shift+L. Just in case.
- heyitsnick 16y agoHa thank you! I was typing the pipe character that's in the top left (alt gr + `) that looked the same but is clearly a different character code. It's a bew laptop with a new keyboard layout (US).
- Fice 16y agoReminds me of the threading macros (-> and ->>) from Clojure.
- geophile 16y agoI have built something similar, coming at the problem from a different direction. I wanted a Unix-style shell, but piping objects instead of strings. For example, to sum x^2 from the command line, x = 0-9: 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. http://geophile.com/osh http://geophile.com/osh
- iki23 16y agoNice! Would deserve a github repository and keeping the pypi pakcage up-to-date: http://pypi.python.org/pypi/osh/ http://pypi.python.org/pypi/osh/
- geophile 16y agoYes, the pypi entry is obsolete. For some reason, I get into some sort of endless lost username/password purgatory whenever I try to register there. I really should bring that up to date and put the package in github.
- jhuni 16y agoInfix notation is more readable then poorly written "classical" prefix notation.
- dfhjkyuk 16y agoONLINE STORE: ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== Air jordan(1-24)shoes $30 Handbags(Coach l v f e n d i d&g) $35 Tshirts (Polo ,ed hardy,lacoste) $15 Jean(True Religion,ed hardy,coogi) $30 Sunglasses(Oakey,coach,gucci,A r m a i n i) $15 New era cap $12 Bikini (Ed hardy,polo) $20 accept paypal and free shipping ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )==== ====== ( http://www.fullmalls.com http://www.fullmalls.com )====
- algorias 16y agoI wonder about the usefulness of this. Doesn't the python generator syntax take care pretty well of most use-cases? 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)
- lloeki 16y agoInserting/disabling/swapping intermediate processing steps in between is immensely easier with pipes, and you don't have to take care of balanced parentheses.
- xtacy 16y agoA similar question was asked on Stackoverflow: http://stackoverflow.com/questions/4961017/clojure-style-function-threading-in-python http://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.
- scott_s 16y agoHey, fogus and btilly! Small world.
- riobard 16y agoThe Fibonacci generator hurts my eyes! Here is a cleaner version. 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 // 4613732
- moomba 16y agoThats great, now if they added some of the other lisp features (macros, etc.) I'd consider using python.
- zacharyvoase 16y agoI wrote a really similar library called Calabash a short while ago: https://github.com/zacharyvoase/calabash https://github.com/zacharyvoase/calabash 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.
- jerf 16y agoISTR that when people discussed various ways of overloading the pipe on comp.lang.python back in the day, one thing you have to be careful of is that the bitwise OR operator binds relatively weakly [1], and that can result in some surprising behavior if you intuitively assign the | a precedence in your head more suited to the behavior implemented here. In this particular case, though, you probably wouldn't hit this problem very often... though in some ways that can make the problem worse in its own way. [1]: http://docs.python.org/reference/expressions.html#summary http://docs.python.org/reference/expressions.html#summary
- lloeki 16y agoMentally making a note of that. I was wondering what kind of side effects the use of | would end up having, if any.
- VMG 16y agoThis calls for allowing the entirely user-defined $-operator in python 4.
- nyellin 16y agoWhat if two Python-based frameworks use $ for separate purposes? (e.g. Django and NumPy)
- OnesimusUnbound 16y agoNice source code, readable, and well documented. The author uses generators when it makes sense. I try to use ipython and pipe as my shell to see how it feels.