5 ms·
I don't know, I often miss the pipe operator in python, which makes certain things much easier. I once created a small class to simulate this a bit, it allows y
by pinopinopino 7y ago
I don't know, I often miss the pipe operator in python, which makes certain things much easier.
I once created a small class to simulate this a bit, it allows you to use + as pipe operator. Wouldn't use it in production though, was just fun to write it. https://pastebin.com/eQacwLj7 https://pastebin.com/eQacwLj7
- ehsankia 7y agoWhy not use | as the pipe operator? Just use __or__ instead of __add__ I have done something similar too once in a shell-like Python CLI I worked on. Can also use __ror__ to be able to pass primitives into your commands like `[1, 2, 3] | SumCmd() | PrintCmd()` I do agree that typing something like that, especially when working in a shell, is much nicer than having to go back and forth all the time to type `print(sum([1,2,3]))` Here it is mentioned in a talk about Python Aesthetics by Brandon Rhodes: https://www.youtube.com/watch?v=x-kB2o8sd5c&t=8m24s https://www.youtube.com/watch?v=x-kB2o8sd5c&t=8m24s
- pinopinopino 7y agoOh that is possible? I am not that deep into python honestly. Looks much neater to use |.
- swiley 7y agoDoesn’t python have channels? The pipe is essentially a channel combined with a close message (a null object I guess) Otherwise you can do something similar with iterators and function composition. I guess the syntax isn’t quite as easy to read if you’re not used to function composition?(which seems surprisingly difficult for some beginners)
- fiddlerwoaroof 7y agoShell pipes are fairly low overhead because they are an os primitive.
- swiley 7y agoI really wouldn’t call pipes “low overhead.”
- andrewshadura 7y agoTried python-sh?
- emmelaich 7y agoOne thing I like Ruby for over Python is the function chaining, which is quite shell-pipe-ish. I'll stick to Python, but Ruby is worth a look.
- krazykringle 7y agoPlumbum (above) matches this need in Python. It overloads | to make pipes! It makes it relatively trivial to express shell idioms in Python.