Skip to content

Instantly share code, notes, and snippets.

@admackin
admackin / lowerCamelCase.py
Last active September 4, 2015 13:44
Convert from underscores to camel case, if you randomly change conventions
# converts the input string with underscores from eg lower_camel_case to lowerCamelCase (do you like what I did with the name there?)
# should work reasonably on anything with vaguely C-like syntax (not LISP probably, but who really cares?)
from contextlib import nested
def camelCaseWord(underscored):
output_vals = []
prev_idx = 0
und_idx = underscored.find('_')
while und_idx != -1:
@admackin
admackin / gist:1136200
Created August 10, 2011 05:35
Force US Letter paper size in PDFlatex
% commands to get pdflatex to output in letter format
\special{papersize=8.5in,11in}
\setlength{\pdfpageheight}{\paperheight}
\setlength{\pdfpagewidth}{\paperwidth}
@admackin
admackin / mysql-repl-check.py
Created August 10, 2011 05:37
Cron script to check for MySQL replication problems
#!/usr/bin/python -Wignore::DeprecationWarning
"""
Call this as a cron script to check for replication problems
"""
import MySQLdb
DB_HOST = '127.0.0.1'
DB_USER = 'USERNAME'
DB_PASSWD = 'PASSWORD'
@admackin
admackin / mysql-backup.sh
Last active October 25, 2022 08:54
Rolling MySQL database backups
#!/bin/sh
# use with cron entries such as:
#3 */2 * * * $HOME/bin/mysql-backup.sh dbname backupname hourly H
#8 1 * * * $HOME/bin/mysql-backup.sh dbname backupname daily a
#33 2 1,8,15,23 * * $HOME/bin/mysql-backup.sh dbname backupname weekly d
#33 2 28 * * $HOME/bin/mysql-backup.sh dbname backupname monthly b
DB_NAME="$1"
BACK_ROOT="$2"
@admackin
admackin / gist:1318551
Created October 27, 2011 01:36
Install packages from last year's BasicTeX distribution in this year's.
grep install /usr/local/texlive/2010basic//texmf-var/web2c/tlmgr.log| perl -pi -e 's/.*install: //' | sudo xargs tlmgr install
@admackin
admackin / replace-command.tex
Created October 31, 2011 06:35
Replace Latex Command
%The following's adapted from the TeX Frequently Asked Questions - Rather than overwriting an old command it's common to want to add some code at the beginning or the end of it. Suppose we want a version of a command that does some small extension of its original definition: we might try:
\renewcommand{\splat}{addedcode\splat}
%However, this would not work: a call to \splat would execute addedcode, and then call the redefined \splat again; this is an infinite recursive loop. Fortunately, the TeX primitive \let command comes to our rescue; it allows us to take a "snapshot" of the current state of a command, which we can then use in the redefinition of the command. So:
\let\Oldsplat\splat
\renewcommand{\splat}{addedcode\Oldsplat}
%effects the required patch, safely. Adding things at the end of a command works similarly.
%If \splat takes arguments, one must pass them on:
\renewcommand{\splat}[2]{addedcode\Oldsplat{#1}{#2}}
@admackin
admackin / check-free-space.py
Last active September 28, 2015 09:58
Check free space and print error message if it is below a threshold
#!/usr/bin/env python
from __future__ import division
import os
import sys
import optparse
"""
Designed for use with cron scripts, as in the default state it only prints output
when the free space it below the requested threshold.
@admackin
admackin / gzipabstract.py
Created February 7, 2012 02:09
Handle Gzipped files semi-transparently
import gzip
import os
import errno
import codecs
from contextlib import nested
GZ_SUFF = '.gz'
BUFSIZE = 1048576
def gzip_fname(filename):
@admackin
admackin / meshparse.py
Created February 22, 2012 06:13
Parse the 'desc' XML from MeSH (Medical Subject Headings) into Python objects
from xml.etree import cElementTree as elemtree
from datetime import date
"""
Use this to parse XML from MeSH (Medical Subject Headings). More information
on the format at: http://www.ncbi.nlm.nih.gov/mesh
End users will primarily want to call the `parse_mesh` function and do something
with the output.
"""
@admackin
admackin / setclasspath.sh
Created March 21, 2012 01:07
Set Java CLASSPATH for a set of library Jars using BASH
#!/bin/sh
# use this for any package (such as ClearParser) which requires setting the
# classpath to a particular set of jar files
if [ -z $BASH_SOURCE ] ; then
echo "Script must be sourced (using '.' or 'source') and run under bash >= 3.0"
exit 1
fi
script_path="${BASH_SOURCE%/*}"