2011-03-30

Run rsync persistently

Sometimes I have to transfer large numbers of large files for work. When an rsync error occurs in the middle of the night, it makes for a pretty disappointing morning. The following script runs rsync persistently, meaning it will retry (with --ignore-existing) until all files are received and rsync exits with status 0.

Here's the script:

#!/bin/bash
# persistent-rsync
# run an rsync command until it completes with exit status 0

# always runs with --progress and --ignore-existing
# passes all other command line arguments on to rsync


COMMAND="rsync --progress --ignore-existing $*"

date
echo "Running rsync until complete..."
echo $COMMAND

while ! $COMMAND
do
    date
    echo "Restarting rsync..."
    sleep 1
done

date
echo "Rsync complete."

exit 0

2011-03-07

Python: PyROOT + optparse

If for some reason you have to use PyROOT (in my case, I need to use TMVA), you probably still want to parse command line arguments for yourself, e.g. with optparse. To prevent PyROOT from scooping up the command line, import it like so:

import sys

tmpargv = sys.argv[:]             # [:] for a copy, not reference
sys.argv = []
from ROOT import gSystem, gROOT
from ROOT import TMVA             # otherwise I wouldn't be using ROOT!
sys.argv = tmpargv

from optparse import OptionParser

Et voilá, your command line is under control!

2011-02-10

Separate input per window in Irssi

In Irssi, "the client of the future", the input box behaves differently by default from the input box in other clients, for example XChat. In XChat, each window or tab has it's own input buffer, so you can draft a statement in one window while still conversing separately in other windows. In Irssi, the default behavior is to for there to be only one input buffer, which persists as you change windows.

To get a separate input buffer for each window (while still pooling the input history), see this script.

2011-01-28

Color control with matplotlib

Sometimes it's important to control the color of lines drawn with matplotlib. Two examples I encountered today were drawing a number of horizontal or vertical lines on the same axes and drawing more lines than the number of unique colors in the default color ring. Here's how you can use a colormap to get the desired number of colors:

import matplotlib.pyplot as plt
cmap = plt.get_cmap ('gist_rainbow')
num_colors = 10
for i in xrange (num_colors):
    plt.axhline (i, color = cmap (1.0 * i / num_colors))

2010-11-01

to title case, at the shell

This filter turns the input into "Title Case".

cat [whatever] \
| sed 's/.*/\L&/; s/[a-z]*/\u&/g'

Courtesy of this post.

2010-08-26

Python string concatenation

Looks like the fastest way to assemble your strings, in general, is by generating lists and then calling '{separator}'.join ({string list}), according to Oliver Crow.