3 ms·
Does anyone have a link to a guide on how to write Python or node or rust programs that behave well with bash? Ie. Streaming inputs and outputs and other thing
by Waterluvian 4y ago
Does anyone have a link to a guide on how to write Python or node or rust programs that behave well with bash? Ie. Streaming inputs and outputs and other things I probably don’t know about?
- deleted 4y ago[deleted]
- KMnO4 4y agoIt’s pretty easy. You have three basic streams: 1. Stdin - just iterate through sys.stdin 2. Stdout - regular printing will go there 3. Stderr - print errors here eg with print(…, file=sys.stderr) And then beyond that as long as your script gets invoked by the interpreter (Ie #!/usr/bin/env python) everything will “just work”.
- IgorPartola 4y agoDon’t you also have to keep in mind how often you flush outputs/how you buffer? Encoding? Handle EOF correctly? Not saying it’s hard but also it’s not 100% covered by what you said.
- markrages 4y agoThose are advanced topics and you can look them up if you need them. Generally, Python does the right thing by default for scripting use: line buffered, system encoding, EOF handled naturally by the iterator protocol.
- gnubison 4y agoAnd preferably use fileinput for the stdin so that you can name files on the command line as well
- Calzifer 4y agoAnd avoid seek. Pipes are not random access. I once tried to use a python library to convert a file from stdin but it failed on a f.seek(0) the library added 'just in case' in the beginning.
- eyelidlessness 4y agoThis is oddly something that some of the earliest Node interfaces do quite well. (I say “oddly” because Node was mostly promoted early on for network/server use cases.) It’s generally not idiomatic in these days of async/await and Web Streams, but streaming IO was a core async primitive from very early on. 0.1.90 for child processes, unspecified for the main process object so possibly from the first release. Granted the interfaces really show their age in terms of incidental complexity, they’re far from being as simple as their shell equivalents. But as far as behaving well, streaming is solid and there’s a wealth of compatibility affordances depending on how portable your script needs to be.
- zokier 4y agoFor Python using fileinput module goes long way: https://docs.python.org/3/library/fileinput.html https://docs.python.org/3/library/fileinput.html
- Too 4y agoWith argparse.FileType, similar behavior integrates well with argparser https://docs.python.org/3/library/argparse.html#argparse.FileType https://docs.python.org/3/library/argparse.html#argparse.Fil...
- jeroenjanssens 4y agoMy book Data Science at the Command Line has a chapter about this that scratches the surface and lists some resources in case you want to dive deeper [1]. I can also recommend checking out packages such as Rich [2] and Click [3], if only to get an idea of the possibilities when it comes to creating command-line tools with Python. [1] https://datascienceatthecommandline.com/2e/chapter-4-creating-command-line-tools.html https://datascienceatthecommandline.com/2e/chapter-4-creatin... [2] https://github.com/Textualize/rich https://github.com/Textualize/rich [3] https://click.palletsprojects.com/en/8.1.x/ https://click.palletsprojects.com/en/8.1.x/