Skip to content

Instantly share code, notes, and snippets.

View benslavin's full-sized avatar

Ben Slavin benslavin

View GitHub Profile
@benslavin
benslavin / generate_sequence.py
Created October 4, 2012 11:40
Generate a generative sequence of numbers from 0 to size, collapsing multiples of N.
import sys
def generate_sequence(size, N=3):
if size == 1:
return[[1]]
else:
accumulator = generate_sequence(size-1, N)
# First, increment the value of the array by 1
next = list(accumulator[-1])
@benslavin
benslavin / csv_import.py
Created August 21, 2012 17:09
Read CSV exports (from Sequel Pro) into a db object
import csv
file_names = [] # THE CSV FILES TO BE PROCESSED
def key_from_filename(filename):
table_name, _ext = filename.rsplit('.', 1)
_prefix, key = table_name.split("_", 1)
return key
def read_files(files=file_names):
@benslavin
benslavin / css3_colors.py
Created February 22, 2012 20:57
CSS3 Color Lookup and Inversion
import string
INVERSION = string.maketrans('0123456789abcdef', 'fedcba9876543210')
__all__ = ['COLOR_LOOKUP', 'COLOR_REVERSE', 'invert']
# See http://www.w3.org/TR/css3-color/#svg-color for the W3C list
COLOR_LOOKUP = {
"aliceblue": "f0f8ff",
"antiquewhite": "faebd7",
"aqua": "00ffff",
def encode(ip_address):
return reduce((lambda ip, part: (ip << 8) | int(part)), ip_address.split('.'), 0)
def decode(addr):
return '.'.join(map(lambda (bits, ip): str((ip >> bits) & 255), [(i*8, addr) for i in range(4)])[::-1])
def test():
TEST_IPS = ["0.0.0.0", "0.1.2.3", "1.1.1.1", "10.11.12.13", "127.0.0.1", "172.16.255.1", "192.168.1.1", "255.255.255.255"]
for test_subject in TEST_IPS:
print "Testing %s ..." % test_subject,