Npcli: Interacting with numpy from the command line

Introducing my new tool npcli, and easy way of interacting with numpy from the bash command-line.

I love shell one-liners. For one off tasks, they let you incrementally build a working solution quickly without opening your editor, finding a file, making it executable, etc etc. There’s something wonderful about continually adding more and more things to a line until it does precisely what you want. To see the value of one-liners you might like to consider the existence of entire websites dedicated to them.

Now, when writing shell one-liners, I often find I hit a wall due to bash’s inability to do maths. npcli is an attempt to allow you to do slightly more in your shell one-liners before having to open an editor.

This is achieved by giving you access to numpy expressions from the bash command-line, so you can write things like seq 100 | npcli 'sum(d)' to sum the numbers from 1 to 100. See these examples for potentially compelling use cases.

More generally, npcli swallows a class of command-line tools used for specific mathematics problems (e.g. summing numbers, creating random data, graphing, averages), in one fell swoop.

Examples of using npcli

# The squares of the numbers 1 to 100
seq 100 | npcli 'd**2'

# Work out the mean of some random numebrs
npcli 'np.random.random(10000)' -m numpy.random | npcli 'np.mean(d)'

# Plot a graph
seq 100 | npcli -nK 'pylab.plot(d);'

# Produce a histogram of when most lines in syslog are printed
sudo cat /var/log/syslog | cut -d " " -f 1-4 | xargs -L 1 -I A date -d A +%s | npcli 'd % 86400' | npcli 'd // 3600 * 3600' | uniq -c | npcli -Kn 'pylab.plot(d[:,1], d[:,0]);'

# Generate some random data
npcli -K 'random(100)'

# Summarize the last 100 days of GOOG's share price
curl "" | head -n 100 | npcli -I pandas 'd["Close"].describe()' -D

# Chain together operations
seq 10 | npcli 'd' -e 'd*2' -e 'd + 4' -e 'd * 3' -e 'd - 12' -e 'd / 6'

# Multiple data sources
npcli --name one <(seq 100) --name two <(seq 201 300) 'one + two'