Skip to content

Instantly share code, notes, and snippets.

@mhayes
mhayes / puzzle.py
Created January 28, 2011 02:58
Python translation capabilities
"""
Decode a super-secret message
"""
import sys
from string import maketrans
# setup translation
in_wrds = "abcdefghijklmnopqrstuvwxyz"
out_wrds = "cdefghijklmnopqrstuvwxyzab"
trans_tbl = maketrans(in_wrds, out_wrds)
@mhayes
mhayes / mess.py
Created January 28, 2011 03:01
Locates alpha characters in a text file
# solution 1 - nested loops
temp = ""
f = open('mess.txt', 'r')
for l in f:
for c in l:
if c.isalpha():
temp += c
print temp
# solution 2 - list comprehension
@mhayes
mhayes / tag_filter.py
Created February 14, 2011 18:07
Finding a set of tags meeting certain criteria
import re
import gdata.photos.service
user = 'intakescreensinc'
service = gdata.photos.service.PhotosService()
tags = service.GetUserFeed(user=user, kind='tag').entry
cfs = re.compile("^(\d+)cfs$")
cfs_sizes = []
for tag in tags:
@mhayes
mhayes / photo.py
Created February 14, 2011 20:35
Retrieve photos from Picasa
import re
import gdata.photos.service
class Collection:
def __init__(self, username):
self.feed = "/data/feed/api/user/%s" % username
self.service = gdata.photos.service.PhotosService()
def fetch(self, feed):
"""Make a call to the photo service."""
return self.service.GetFeed(feed)
@mhayes
mhayes / deploy.rb
Created March 28, 2011 06:16
Capistrano configuration for WebFaction
require "bundler/capistrano"
# use ssh key from local system
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :user, "markhayes"
# set :password, ""
set :domain, "markhayes.webfactional.com"
set :application, "trackr"
@mhayes
mhayes / pla.rb
Created April 12, 2011 19:07
Takes espresso output and converts to friendly PLA input format (for use in LogicWorks)
#!/usr/bin/env ruby
require 'optparse'
options = {}
optparse = OptionParser.new do |opts|
opts.on('-f', '--file FILE', 'Specify espresso output file') do |file|
options[:espresso_file] = file
end
opts.on('-h', '--help', 'View help') do
@mhayes
mhayes / bst.rb
Created May 1, 2011 05:45
Binary Tree Recursion
require 'test/unit'
class Node
attr_accessor :value, :lchild, :rchild
def initialize(val=nil)
@value = val
end
def depth
@mhayes
mhayes / rotate.rb
Created May 3, 2011 23:23
Ability to rotate characters in a string
class String
#rotate characters n positions to left or right
def rotate(n)
alpha = ("a".."z").to_a + ("A".."Z").to_a
self.tr(alpha.join(""), alpha.rotate(n).join(""))
end
def rotate!(n)
replace rotate(n)
end
@mhayes
mhayes / bit_chop.rb
Created May 7, 2011 06:58
Sweet bit chopping
# convert n from base10 to base2
# return base10 representation of
# k least significant bits
# $> bitwise_chop(29,3) => 5
def bitwise_chop(n,k)
n.to_s(2).slice(-k,k).to_i(2)
end
@mhayes
mhayes / email.rb
Created May 9, 2011 20:10
Proof of concept e-mail tracking
require 'rubygems'
require 'open-uri'
require 'sinatra'
get '/signature.png' do
content_type 'image/png'
File.open("stats.txt", "a") {|f| f.write request.env.select {|k,v| ["HTTP_USER_AGENT", "REMOTE_HOST"].include? k}}
open("http://s3.deployfx.com/mhayes-resume/images/mhayes-signature.png") {|image| image.read}
end