Skip to content

Instantly share code, notes, and snippets.

@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 / remove_pyc.py
Last active April 26, 2021 21:30
Remove all *.pyc files under the given path recursively
#!/usr/bin/env python
"""
Remove all .pyc files in the path recursively
"""
from __future__ import print_function
import argparse
import os
import shutil
@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 / canadapost.py
Last active July 14, 2016 16:04
Finds Canada Post rates to send a parcel or oversized/non-standard envelope domestically, to the US, or internationally. Particularly useful for people selling items on eBay. It will print our rates to different destinations around the country/US/world so you can get a feel for the min/average/max rate.
#!/usr/bin/env python3
import argparse
import base64
import xml.etree.ElementTree as ET
import urllib.request, urllib.error, urllib.parse
import operator
import time
from canadapostconf import username, password
@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 / 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 / check_vancouver_swimming_lessons.py
Last active December 18, 2015 04:39
Checks swimming lessons availability with the City of Vancouver.
#!/usr/bin/env python2
"""
Search for swimming lessons on the Vancouver Aquatic Services web page
that match certain criteria.
"""
import urllib2
import json
#WP=Lord Byng