Skip to content

Instantly share code, notes, and snippets.

@pauladam
pauladam / gist:226467
Created November 4, 2009 22:59
Simple script for remotely shutting down your DS nas device. Im lazy so I like to avoid logging into the web ui and clicking around.
#!/usr/bin/python
import urllib,urllib2, sys
BASE_URL = 'http://192.168.100.101:5000'
ADMIN_PASS = 'YOUR ADMIN PASS'
# Login to admin interface
parameters = {'username' : 'admin', 'passwd':ADMIN_PASS,'service_type':'1'}
request = urllib2.Request(BASE_URL+'/webman/modules/login.cgi', urllib.urlencode(parameters))
@pauladam
pauladam / gist:304295
Created February 14, 2010 22:03
Random choice with weighted probabilities
# typical to sum over your list of items perhaps
# performing some calculation on each item
normalizing_constant = \
sum([calc(item) for item in items if filter(item)])
r = random.random()
for item in items:
prob = get_probability(item) / normalizing_constant
from django import forms
class SomeForm(forms.Form):
visible = forms.DecimalField(label="Amount")
hidden = forms.IntegerField(widget=forms.HiddenInput)
# Bulk add a bunch of urls to your Instapaper account
$ for link in `cat links.txt`; do curl --basic --user username:password --data url=$link http://www.instapaper.com/api/add; done
@pauladam
pauladam / partial_function_application.js
Created March 10, 2011 01:29
Practical example of function currying in JS
// I have some code that needs to use the following
// construct repeatedly
for(i=0;i<classes.length;i++){
var c = classes[i];
for(j=0;j<centers.length;j++){
var k = centers[k];
// do something with [c,k], like,
// demand[[c,k]] = expression;
@pauladam
pauladam / reckon seconds till next tuesday.coffee
Created October 20, 2011 00:32
Seconds till next tuesday (coffeescript)
# returns seconds till next tuesday @ noon - 5 minutes
exports.reckonTimeoutValue = ()->
# num days till next tuesday
tuesOffset =
2 : 7
3 : 6
4 : 5
5 : 4
6 : 3
@pauladam
pauladam / copy-safari-tabs.rb
Created November 21, 2011 19:54
Copy open safari tabs to Chrome
#!/usr/local/bin/macruby
framework "ScriptingBridge"
safari = SBApplication.applicationWithBundleIdentifier("com.apple.Safari")
safari.windows.each {|window|
window.tabs.each { |tab|
cmd = "open -a '/Applications/Google\ Chrome.app/' #{tab.URL}"
`#{cmd}`
}
@pauladam
pauladam / ripit.sh
Created February 11, 2012 22:53
Barebones cd -> mp3 bash script
#!/bin/bash
cdparanoia -B
ls | while read fn; do lame $fn; done;
mp3wrap -v album.mp3 *.mp3
mp3val album_MP3WRAP.mp3 -f -nb
mv album_MP3WRAP.mp3 album.mp3
cp album.mp3 ~/Dropbox/Public/
[12] pry(main)> class Fixnum
[12] pry(main)* def in(l)
[12] pry(main)* l.include? self
[12] pry(main)* end
[12] pry(main)* end
=> nil
[13] pry(main)> 1.in [1,2,3]
=> true
@pauladam
pauladam / gif-it.rb
Created September 22, 2012 20:14
movie -> gif89a
#!/usr/bin/ruby
# gif-it
if ARGV.length < 3
puts "usage: gif-it <filename> <start-time> <duration>"
end
fn = ARGV.shift
start_time = ARGV.shift
duration = ARGV.shift