This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Oran Looney, November 2011 | |
""" GENERAL SECTION """ | |
" turn on automatic syntax highlighting. | |
syntax on | |
filetype plugin on | |
set directory=/tmp | |
""" WHITESPACE """ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer