Skip to content

Instantly share code, notes, and snippets.

@olooney
olooney / operators.js
Created May 28, 2011 20:13
javascript operators wrapped in functions so you can refer to them by name
op = {
"!!": function(x) { return !!x; },
"!": function(x) { return !x; },
"&&": function(x, y) { return x && y; },
"||": function(x, y) { return x || y; },
"~": function(x) { return ~x; },
"negative": function(x) { return -x; },
"+": function(x, y) { return x + y; },
@olooney
olooney / addEventListener.js
Created June 15, 2011 15:39
cross-browser event listeners
// observable is a DOM node, document, window, etc.
// eventName is the string event name, say "load" or "click"
// handler is the callback function.
function addEventListener(observable, eventName, handler) {
if ( observable.addEventListener ) {
observable.addEventListener(eventName, handler, false);
} else if ( observable.attachEvent ) {
observable.attachEvent("on" + eventName, handler);
} else {
// error handling?
@olooney
olooney / sizes.cpp
Created August 8, 2011 18:59
Show the sizes of C++ built-in types (useful for 32/64 bit systems)
#include <iostream>
#define SIZE(type) std::cout << #type ": " << sizeof(type) << " bytes\n";
int main(int argc, char** argv ) {
SIZE(char);
SIZE(short);
SIZE(int);
SIZE(long);
SIZE(float);
@olooney
olooney / average_image_color.py
Created September 27, 2011 21:14
Average Image Color
from PIL import Image
def average_image_color(filename):
i = Image.open(filename)
h = i.histogram()
# split into red, green, blue
r = h[0:256]
g = h[256:256*2]
b = h[256*2: 256*3]
@olooney
olooney / .virmrc
Last active October 26, 2017 09:57
" Oran Looney, November 2011
""" GENERAL SECTION """
" turn on automatic syntax highlighting.
syntax on
filetype plugin on
set directory=/tmp
""" WHITESPACE """
@olooney
olooney / pkcs7.py
Created December 19, 2011 17:15
padding/unpadding per PKCS7 or PKCS5.
# padding/unpadding per PKCS7 or PKCS5.
class PKCS7(object):
def __init__(self, block_size):
self.block_size = block_size
def pad(self, string):
padding_number = self.block_size - len(string) % self.block_size
if padding_number == self.block_size:
return string
@olooney
olooney / cropped_thumbnail.my
Created January 12, 2012 16:31
A "better" thumbnail algorithm for Python Image Library PIL Image
'''
PIL's Image.thumbnail() returns an image that fits inside of a given size (preserving aspect ratios)
but the size of the actual image will vary and is certainly not guaranteed to be the requested size.
This is often inconvenient since the size of the returned thumbnail cannot be predicted. The django-thumbs
library solves this for square thumbnails by cropping the image to a square and then resizing it. However,
this only works for exact squares.
This function generalizes that approach to work for thumbnails of any aspect ratio. The returned thumbnail
is always exactly the requested size, and edges (left/right or top/bottom) are cropped off to adjust to
make sure the thumbnail will be the right size without distorting the image.
@olooney
olooney / make_data_url.py
Created February 14, 2012 22:41
simple python script to generate an inline data: URL for a jpeg image from a file
def make_data_url(filename):
prefix = 'data:image/jpeg;base64,'
fin = open(filename, 'rb')
contents = fin.read()
import base64
data_url = prefix + base64.b64encode(contents)
fin.close()
return data_url
@olooney
olooney / p2.py
Created February 22, 2012 15:42
examples of python class customization, generic descriptors
import math
class Magnitude(object):
'''A descriptor that can be added to any N-dimensional point to provide a read/writable magnitude property.'''
def __init__(self, *attributes):
self.attributes = attributes
def __get__(self, instance, owner):
sum_of_squares = 0.0
@olooney
olooney / encrypt-decrypt.bash
Created February 29, 2012 22:33
Use openSSL to simply encrypt a file
# snippet from http://www.madboa.com/geek/openssl/#encrypt-simple
# encrypt file.txt to file.enc using 256-bit AES in CBC mode
openssl enc -aes-256-cbc -salt -in file.txt -out file.enc
To decrypt file.enc you or the file’s recipient will need to remember the cipher and the passphrase.
# decrypt binary file.enc
openssl enc -d -aes-256-cbc -in file.enc