Skip to content

Instantly share code, notes, and snippets.

View sj26's full-sized avatar
🤹‍♂️

Samuel Cochran sj26

🤹‍♂️
View GitHub Profile
@sj26
sj26 / prime-gen.py
Created February 21, 2011 07:04
The fastest way I could implement Erastothenes prime number sieve in Python.
import sys, collections
cases = int(sys.stdin.readline().strip())
for case in range(1, cases + 1):
candidate_min, candidate_max = map(int, sys.stdin.readline().strip().split())
# Python is good at sparse dictionaries, so use a bubbling
# Sieve of Erastothenes to be memory efficient and fairly
# computationally efficient
composites = collections.defaultdict(list)
for candidate in range(2, candidate_max + 1):
if candidate not in composites:
@sj26
sj26 / upcdb.py
Created March 16, 2011 06:47
Python 3 command line interface to http://upcdatabase.com
#!/usr/bin/env python3
import os, sys
import xmlrpc.client
rpc_key = '...' # replace with your RPC Key from http://upcdatabase.com/user
server = xmlrpc.client.ServerProxy('http://www.upcdatabase.com/xmlrpc')
lookup_keys = {12: 'upc', 13: 'ean'}
@sj26
sj26 / admin_controller.rb
Created April 9, 2011 14:44
Nice nested layouts -- this does what *I* expect, at least.
class AdminController < ApplicationController
layout :admin
def index
render :dashboard
end
end
@sj26
sj26 / parseDuration.js
Created April 20, 2011 06:50
Parses a natural duration string into seconds.
// Try to be as sensible as possible about parsing durations
function parseDuration(duration) {
// .75
if (match = /^\.\d+$/.exec(duration)) {
return parseFloat("0" + match[0]) * 3600;
// 4 or 11.75
} else if (match = /^\d+(?:\.\d+)?$/.exec(duration)) {
return parseFloat(match[0]) * 3600;
// 01:34
} else if (match = /^(\d+):(\d+)$/.exec(duration)) {
@sj26
sj26 / app1.ru
Created May 13, 2011 03:06
Multiple rack applications mounted inside each other
require 'rack'
use Rack::ContentLength
use Rack::ContentType
run proc { |env| [200, {}, '<p>First application</p><p><a href="/bar">Go to Second Application</a></p>'] }
Around do |scenario, block|
# We can't use .begin/rollback_db_transaction as they don't do the
# savepoint emulation on MySQL, it's all encapsulated in
# .transaction
result = nil
ActiveRecord::Base.transaction(:requires_new => true) do
result = block.call
raise ActiveRecord::Rollback
end
result
Around do |scenario, block|
# We can't use .begin/rollback_db_transaction as they don't do the
# savepoint emulation on MySQL, it's all encapsulated in
# .transaction
result = nil
ActiveRecord::Base.transaction(:requires_new => true) do
begin
result = block.call
ensure
raise ActiveRecord::Rollback
def destroy
authorize! :destroy, @health_professional
@health_professional.destroy
respond_with :manage, @health_professional
end
require 'headless'
# Wrap in headless to make sure we have an xvfb
AfterConfiguration do
# Base display number on current PID
headless = Headless.new :display => Process.pid
headless.start
at_exit do
@sj26
sj26 / application.rb
Created June 7, 2011 01:14
Rails 3.1 compass initializer
config.generators.stylesheet_engine = :sass