Skip to content

Instantly share code, notes, and snippets.

View johnpena's full-sized avatar

John Peña johnpena

View GitHub Profile
@johnpena
johnpena / crap.py
Created October 15, 2014 03:22
Lexical scoping doesn't work like that in python
def decorator_function(original_function):
# x will not be accessible inside functions decorated by `decorator_function`
x = "certified organic and local"
def inner():
return original_function()
return inner
@johnpena
johnpena / typing.py
Last active August 29, 2015 14:05
Crappy runtime typing for python
import functools
class ArgumentLengthMismatchException(Exception):
pass
class InvalidParameterTypeException(Exception):
pass
def get_user_from_db(username):
statement = "SELECT * FROM users WHERE name ='" + username + "';"
cursor.execute(statement)
return cursor.fetchall()[0]
get_user_from_db("'; DROP TABLE users; '")
@johnpena
johnpena / pushStackFix.js
Created July 25, 2012 01:42
fix for pushstack issues in jquery+chrome
(function () {
jQuery.fn.pushStack = function( elems, name, selector ) {
// Build a new jQuery matched element set
var ret = this.constructor();
// temporary workaround for chrome issue #125148
// http://code.google.com/p/chromium/issues/detail?id=125148
if (!(ret instanceof jQuery.fn.init)) {
@johnpena
johnpena / decorators.py
Created June 17, 2011 01:13
Understanding decorators
@my_decorator
def foo(bar):
return bar
# IS EXACTLY THE SAME AS
def foo(bar):
return bar
foo = my_decorator(foo)
@johnpena
johnpena / self.rb
Created June 2, 2011 22:09
What happens?
class Foo
class << self
def bar
"baz"
end
end
end
self = Foo.new
self.bar
@johnpena
johnpena / quine.rb
Created May 17, 2011 05:52
quine outputs itself
def quine(source)
puts source + '%Q{' + source + '}'
end
quine %Q{def quine(source)
puts source + '%Q{' + source + '}'
end
quine }
@johnpena
johnpena / loadscript.js
Created February 24, 2011 23:40
Dynamically load a javascript file
function loadScript(src, callback) {
var head = document.getElementsByTagName('head')[0];
var new_script = document.createElement('script');
new_script.type = 'text/javascript';
new_script.src = src;
// most browsers
new_script.onload = callback;
// IE 6 & 7
@johnpena
johnpena / md5rainbow.py
Created February 16, 2011 23:50
Generating a rainbow table for md5
import hashlib
def allstrings(alphabet, length):
""" Get all strings made of characters from `alphabet` up to length of `length`."""
if length <= 0 or len(alphabet) == 0: return []
alphabet = set([alpha for alpha in alphabet])
allstr = set([ch for ch in alphabet])
@johnpena
johnpena / reverse.c
Created February 15, 2011 04:33
Reverse a string in C without using temporary storage
#include <string.h>
void reverse(char* str) {
int i;
int len = strlen(str);
for (i = 0; i < len/2 ; ++i){
str[len] = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = str[len];