Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Last active August 29, 2015 13:56
Show Gist options
  • Save SpotlightKid/42880bb873c5ff9e697a to your computer and use it in GitHub Desktop.
Save SpotlightKid/42880bb873c5ff9e697a to your computer and use it in GitHub Desktop.
Here is a useful little shell function I came up with while experimenting with Cython (http://cython.org/). Put it in your ~/.bashrc and then use it to compile a Cython source code file (*.pyx extension) into a standalone executable and run it directly.
pyxrun() {
PYTHON="${PYTHON:-python}"
if [ "x$1" = "x-t" ]; then
local timeit="time"
shift
fi
if [ -z "$1" ]; then
echo "usage: pyxrun [-t] <file>.pyx"
return 2
else
pyx="$1"
bn="${pyx%.*}"
shift
fi
if [ ! -x "$bn" -o "$pyx" -nt "$bn.c" ]; then
cython --embed "$pyx" && \
gcc -o "$bn" "$bn.c" `$PYTHON-config --cflags --ldflags`
if [ $? -ne 0 ]; then
echo "Compilation of $pyx failed."
return 1
fi
fi
eval $timeit "`dirname $pyx`/${bn##*/}" "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment