It even has addons which enables extras like:
https://mario-addons.readthedocs.io/en/latest/cli_reference....
"xonsh is a shell whose language is a superset of Python; this is more ambitious and pretty different from pyp. pyp is easier to use for the one-liner piping use case, but if you need more Python in your shell, check out xonsh."
It's very useful to be able to automate with a language you already know. I use it quite a bit now, for instance to figure out which of my git repos need a push or a pull.
http://code.activestate.com/recipes/437932-pyline-a-grep-lik...
I'm sure I wasn't the first to scratch that particular itch. It always seemed odd, back then, that Python didn't have better support for the Perl one-liner style of programming.
I already turn to Python in place of Bash whenever any nontrivial amount of complexity is needed or if any reuse is expected in the script I'm writing (I do this with Python Fire). Now I think I'll be making that Bash->Python switch at even lower levels of complexity, and even when no reuse is expected at all.
I use Ruby from the cli frequently for stuff that exceeds an easy jq expression or if jq is not availabel but Ruby is (which happens a lot on RHEL systems when EPEL is not enabled, which is the case in a lot of banks) Ruby's awk -> perl -> ruby heritage endows it with a pretty good cli mode, but sometimes it's verbose. For example parsing json:
curl https://something \
| ruby -r json -e "puts JSON.parse(STDIN.read)['data']['ip']"
But obviously that's wordier than it needs. (To pre-empt the "just use jq!" people, I agree. For simple examples I would just use jq. This example here is just an example).This looks like a big improvement, with math functions auto-imported, and the --script option is a fantastic idea.
Good job.
Well, two things, the second thing being performance.
How's the startup time? I find it hard to get python utils under a few hundred ms just to spin up.
Startup time hasn't been particularly perceptible in my use of it. Just ran a quick benchmark on my (old, not powerful) laptop:
~ λ hyperfine 'pyp x'
Benchmark #1: pyp x
Time (mean ± σ): 75.9 ms ± 1.2 ms [User: 55.9 ms, System: 15.1 ms]
Range (min … max): 74.5 ms … 79.0 ms 36 runs
Since there isn't any input, this should just measure startup and AST transformation time.Surprised it didnt exist already
Not sure if this comment will ever get seen, but I just added the ability to configure pyp with statically analysed Python, so it's easy to define your own functions, import aliases, or use libraries like pipetools to make your life easier! More details at https://github.com/hauntsaninja/pyp#pyp-is-configurable
How would one accomplish the following with pyp?
* Wait until one sees a line with a start marker (say 'start:')
* Perform on an operation on the lines until an end marker ('end:') is reached.
Would you do a whole-input operation on stdin to extract the relevant lines first and then operate on the individual lines? Or would you maintain boolean conditionals within the loop?
pyp 'lines[lines.index("start") + 1:]' | pyp 'lines[:lines.index("end")]' | pyp 'int(x) + 1'
pyp 'start = lines.index("start"); end = lines.index("end", start); map(lambda x: int(x) + 1, lines[start + 1:end])'
pyp 'dropwhile(lambda x: x != "start", lines)' | pyp 'lines[1:]' | pyp 'takewhile(lambda x: x != "end", lines)' | pyp 'int(x) + 1'
pyp 'map(lambda x: int(x) + 1, takewhile(lambda x: x != "end", islice(dropwhile(lambda x: x != "start", lines), 1, None)))'
pyp -b 'cond = False' 'if cond and x == "end": break' 'if cond: print(int(x) + 1)' 'if x == "start": cond = True' perl -lne '$sum += $_ if /start:/ .. /end:/; END{print $sum}' file.txt
With the -n option, perl runs your code for every line in the input (stdin, or filenames). $_ holds the content of the line. And the .. operator is a stateful (!) operator that becomes true when its left part becomes true, until the right part becomes true.So I write 95% of my code in Python, but for command line magic, it's hard to beat Perl.
It seems to have disappeared off the face of the earth, the only thing I found is this fork: https://github.com/fish2000/pythonpy-fork
One thing I'm wondering is how's the startup time/performance in general? awk is kinda slow overall, but Python starts a little too slow to be used in shell scripting.
And sure enough, it's both easy and reasonably fast to run:
$ time seq 1 10000 | awk '{a+=$0}END{print a}'
50005000
seq 1 10000 0.00s user 0.00s system 91% cpu 0.002 total
awk '{a+=$0}END{print a}' 0.01s user 0.00s system 95% cpu 0.008 total
$ time seq 1 10000 | python3 pyp.py 'sum(map(int, lines))'
50005000
seq 1 10000 0.00s user 0.00s system 80% cpu 0.002 total
python3 pyp.py 'sum(map(int, lines))' 0.06s user 0.01s system 99% cpu 0.070 total
I wouldn't run it in a loop, but it's definitely a contender even in high-ish performance situations.