Skip to content

Instantly share code, notes, and snippets.

@dgrant
dgrant / PhotoRenamer.py
Last active December 18, 2015 04:39
Rename photos from something like DSC_1345.jpg, DSC_1346.jpg, DSC_1357.jpg to something like: Hawaii_2002_01.jpg, Hawaii_2002_02.jpg, Hawaii_2002_03.jpg.
#!/usr/bin/env python
import exif
import sys, os, getopt, string, datetime, operator, math
import os.path
from os.path import splitext
EXTS = ['.jpg']
def getFiles(directory):
@dgrant
dgrant / gamblor.py
Last active December 16, 2015 05:49
Just a simulator for the problem posed by Geordie Rose here: http://dwave.wordpress.com/2008/03/03/i-am-gamblor/
from __future__ import division
import random
totalPaid=0
numRuns=0
maxRuns=100000
maxRuns=10000
amountCharged=1
def flipCoins():
result=0
num=-1
@dgrant
dgrant / cache_util.py
Created April 15, 2013 08:36
Python memoize annotation. Add @cachedmethod to any function and the results will be memoized!
import types
def cachedmethod(function):
return types.MethodType(Memoize(function), None)
class Memoize:
def __init__(self,function):
self._cache = {}
self._callable = function
@dgrant
dgrant / profiling_mean_test.py
Created April 15, 2013 08:38
Just calculating the mean on an array and doing some profiling in Python.
# mean of n values within an array
import numpy, time
listSize = 10000
meanLength = 50
def nmean(list,n):
a = []
for i in xrange(1,len(list)+1):
@dgrant
dgrant / sync_music_device.py
Last active December 14, 2015 23:19
Simple GUI-based script to synchronize a music folder with a music device. Haven't tested this in a while, so I'm not sure if this still works.
#!/usr/bin/env python
"""
Synchronize music with a portable device
"""
import os
import subprocess
from easygui import msgbox, choicebox, codebox, ynbox, diropenbox
def sync_music():
@dgrant
dgrant / create_mp3_samples.py
Last active December 14, 2015 23:18
This script will create mp3 sample clips.
#!/usr/bin/env python
"""A utility for creating a clip of an mp3 file"""
import getopt, os, sys
from subprocess import Popen, PIPE
try:
from mutagen.id3 import ID3
except ImportError:
print "Missing mutagen. On Debian systems, install python-mutagen"
sys.exit(1)
@dgrant
dgrant / resize_images.sh
Last active December 14, 2015 22:39
This is an example of how to generate a bunch of resized images
#!/bin/sh
for file
do
newfile=`echo $file | sed -e "s/\(.*\)\.jpg/\1_400\.jpg/g"`
convert -resize 400x400 $file ../images/$newfile
newfile=`echo $file | sed -e "s/\(.*\)\.jpg/\1_150\.jpg/g"`
convert -resize 150x150 $file ../images/$newfile
done
@dgrant
dgrant / debian_find_orphan_files.py
Last active December 14, 2015 22:39
For debian systems, this finds all files that do not belong to a package.
#!/usr/bin/env python3
"""
Search for orphan files (files that do not belong to any package)
"""
import fnmatch
import os
import subprocess
import itertools
INCLUDES = ('/usr', '/etc', '/opt', '/lib32', '/lib64', '/sbin')
@dgrant
dgrant / create_gist_symlinks.py
Last active December 14, 2015 22:38
This script will create symlinks in specific "binpath" for all executables in sub-directories of a specified "gistpath"
@dgrant
dgrant / find_vcs_dirs.py
Last active December 14, 2015 22:29
Find any VCS directories in the specified path (ie. git, mercurial, svn, CVS)
#!/usr/bin/env python3
"""
Find all the VCS controlled directories in a file path
"""
import argparse
import os
EXTS = ['.git', '.hg', '.svn', 'CVS']
def find(path):