Skip to content

Instantly share code, notes, and snippets.

View sneeu's full-sized avatar
🦀
Hi

John Sutherland sneeu

🦀
Hi
View GitHub Profile
@sneeu
sneeu / bracketless.py
Created November 12, 2009 15:15
Bracketless function calls (a la Ruby) in Python.
import inspect
class Bracketless(object):
def __getattribute__(self, name):
try:
attr = object.__getattribute__(self, name)
if callable(attr):
args, __, __, __ = inspect.getargspec(attr)
if len(args) == 1: # 1 for `self`
import Queue
import threading
def do_work(*args):
# Do something with args
pass
number_of_workers = 20
@sneeu
sneeu / values.php
Created February 17, 2010 12:23
What I thought was the first non-trivial example I'd found of PHP being shorter than Python. Saved by Twitter.
<?
/**
* getValues(array('a' => 1, 'b' => 2), array('b', 'c')); # => array('b' => 2)
*/
function getValues($data, $fields)
{
return array_intersect_key($data, array_flip($fields));
}
@sneeu
sneeu / geoplanet_import_psql.sql
Created July 26, 2010 16:21
Import Yahoo GeoPlanet data into PostgreSQL.
-- Schema and import for Yahoo GeoPlanet data into a PostgreSQL database.
CREATE TABLE places (
woe_id VARCHAR(15) PRIMARY KEY,
iso VARCHAR(6),
name TEXT,
language VARCHAR(6),
place_type VARCHAR(15),
parent_woe_id VARCHAR(15)
);
@sneeu
sneeu / cellular_automata.py
Created February 17, 2011 13:56
A somewhat untidy Python script to generate a 1d cellular automata based on an MD5 hash.
"""
Example output: http://www.flickr.com/photos/sneeu/5448646680/
"""
import hashlib
RULES = {
4: {
def counter(start=0, increment=1)
i = start - increment # Smells
lambda do
i += increment
end
end
result = counter(10, 2)
def counter(start, increment):
while True:
yield start
start += increment
result = counter(10, 2)
for __ in xrange(0, 5):
@sneeu
sneeu / configuration.rb
Created April 7, 2011 10:48
Small DSL for SRC charity tutorial.
class AppServer
attr_accessor :port, :admin_password
end
class Configuration
attr_reader :app_server
attr_accessor :tail_logs, :max_connections, :admin_password
def app_server(&block)
@sneeu
sneeu / qsort.py
Created April 27, 2011 18:06
Quick sort in Python.
def qsort(l):
"""
>>> qsort([])
[]
>>> qsort([-1])
[-1]
>>> qsort([1, 2, 3, 4])
[1, 2, 3, 4]
>>> qsort([4, 3, 2, 1])
[1, 2, 3, 4]
------------------------------------------------------------
/usr/local/bin/pip run on Tue May 10 12:46:14 2011
Downloading/unpacking virtualenv
Getting page http://d.pypi.python.org/simple/virtualenv
URLs to search for versions for virtualenv:
* http://d.pypi.python.org/simple/virtualenv/
Getting page http://virtualenv.openplans.org
Getting page http://www.virtualenv.org
Analyzing links from page http://d.pypi.python.org/simple/virtualenv/
Found link http://d.pypi.python.org/packages/source/v/virtualenv/virtualenv-0.8.2.tar.gz#md5=241f157e92dd9052b5a4b43efcef8035 (from http://d.pypi.python.org/simple/virtualenv/), version: 0.8.2