Skip to content

Instantly share code, notes, and snippets.

for i in range(ord('a'), ord('z')):
print "{0:d}\t{0:b}".format(i)
#!/usr/bin/env python
import ID3, os
for fname in [ x for x in os.listdir(".") if x.lower().endswith(".mp3") ]:
print "handling: %s" % fname
tag = ID3.ID3(fname)
new_name = "%0.2d %s.mp3" % (tag.track, tag.title)
if fname and tag and new_name:
os.rename(fname, new_name)
@bensonk
bensonk / tag_parser.rb
Created August 12, 2011 21:07
Tag parsing in ruby
#!/usr/bin/env ruby
tag_samples = [ 'foo bar', 'foo, bar, baz', 'foo "fun times" stuff', '"fail", "foo times"', "foo 'this is fun' bar" ]
tag_samples.each do |tag_str|
tags = tag_str.scan(/(\w+)|("([^"]+)")|('([^']+)')/).map { |a, _, b, _, c| (a or b or c).strip }
puts "#{tag_str}:"
puts tags
end
#!/usr/bin/env python
import serial, time
ser = serial.Serial('/dev/tty.usbserial-FTFB009M', 9600, timeout=1)
while True:
for val in [ "0", "a", "c", "g", "o" ]:
ser.write(val)
for i in range(5):
print ser.readline(),
@bensonk
bensonk / regex_sample.js
Created January 31, 2011 04:12
Regular Expression Example
var stuff = "p> This is some text";
console.log("line: ", stuff);
var re = /p([<>]) (.*)/;
var items = re.exec(stuff);
console.log(items[1]);
@bensonk
bensonk / bk_roman.py
Created December 17, 2010 05:48
Roman Numerals
vals = { "I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000, "_" : 0}
while True:
input = raw_input("Enter a roman numeral: ").upper()
if input == "QUIT": break
input = [ vals[v] for v in input if v in vals ]
value = 0
for cur, next in zip(input, (input+[0])[1:]):
if cur >= next: value += cur
else: value -= cur
print "Value: %d" % value
<%= image_tag "http://maps.google.com/maps/api/staticmap?center=#{suggestion.lat},#{suggestion.lon}&zoom=5&size=170x120&maptype=roadmap&markers=#{suggestion.lat},#{suggestion.lon}&sensor=false" %>
select observations.*, rankings.*
from observations inner join rankings on observations.behavior = rankings.code
where rankings.rank = (select max(rankings.rank)
from observations join rankings on observations.behavior = rankings.code
group by brand);
#!/usr/bin/python
from pprint import pprint
query = "insert into observations(beach, month, date, year, observer, effortid, " + \
"brand, sex, age, behavior, time, photoname, comments) values('%s', '%s', " + \
"'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');"
rawdata = open("UgamakBrands2010.csv").readlines()
for line in [ line.strip().split(",") for line in rawdata[1:] ]:
print query % tuple(line)
Coin Quantity Mass
pennies $0.50 132 g
nickels $2.00 201 g
dimes $5.00 115 g
quarters $10.00 226 g