On Fri, Aug 27, 2021 at 1:15 PM Finn Mason
Is this too magical? result = run('cat file.txt') | run('sort) | run('grep hello', capture_output=True, text=True).stdout
Interesting idea, especially overloading the union/pipe operator (|). I like it a lot. It reminds me of pathlib.Path (which is a wonderful tool), with its slash operator overload. Python is supposed to be easy to read and understand, and anything that makes complicated system stuff less messy is welcome, I think.
It's perfectly legal to do this sort of thing, but it doesn't really belong in the stdlib (more on that later).
Another idea (which I am less in favor of) is a more functional approach, like:
run('grep hello', run('sort', run('cat file.txt')))
That's a bit more viable. I'd be inclined to have a keyword argument here, for instance: run('grep hello', stdin=run('sort', sttdin=run('cat file.txt'))) but I agree, it makes good sense to have an argument able to take a variety of data types, including another program's output. But Python isn't a language that needs everything to be done with command invocations and pipes. If you're doing long pipelines like this, there's probably something wrong. Why use external programs to do things that Python can do far more viably itself? with open('file.txt') as f: result = [l.strip("\r\n") for l in sorted(f) if "hello" in l] Trying to make a more Pythonic syntax for an operation that Python does better in a completely different way is a bit of a half-hearted solution. I'd recommend either sticking to a full "run the shell and hand it this command line", or going the whole way and doing the job in Python. ChrisA