Skip to content

Instantly share code, notes, and snippets.

"""
Two things are wrong with Django's default `SECRET_KEY` system:
1. It is not random but pseudo-random
2. It saves and displays the SECRET_KEY in `settings.py`
This snippet
1. uses `SystemRandom()` instead to generate a random key
2. saves a local `secret.txt`
@sebastibe
sebastibe / unzip_wsgi.py
Created May 31, 2013 11:35
A simple WSGI middleware to unzip HTTP requests when the Content-Encoding header is set to 'gzip'
import logging
from StringIO import StringIO
import zlib
class UnzipRequestMiddleware(object):
"""A middleware that unzips POSTed data.
For this middleware to kick in, the client must provide a value
for the ``Content-Encoding`` header. The only accepted value is
User = Backbone.Model.extend({
url: function() {
var origUrl = Backbone.Model.prototype.url.call(this);
return origUrl += origUrl.endsWith('/') ? '' : '/';
}
});
pb-kill-line () {
zle kill-line
echo -n $CUTBUFFER | pbcopy
}
pb-kill-whole-line () {
zle kill-whole-line
echo -n $CUTBUFFER | pbcopy
}
# credits to http://glowingpython.blogspot.jp/2013/03/bubble-sort-visualized.html
import pylab
from random import shuffle
def bubblesort_anim(a):
x = range(len(a))
imgidx = 0
# bubble sort algorithm
@sebastibe
sebastibe / eletric-pairs.el
Created February 27, 2013 10:15
auto-close any kind of brackets
;; setting for auto-close brackets for electric-pair-mode regardless of current
;; major mode syntax table
(setq electric-pair-pairs '(
(?\" . ?\")
(?\{ . ?\})
) )
;; auto-close any kind of brackets
(add-hook 'after-change-major-mode-hook 'electric-pair-mode)
@sebastibe
sebastibe / warnings_to_exceptions.py
Created February 19, 2013 10:13
During development, you can turn such warnings into exceptions and get a traceback by adding the following to your settings file
import warnings
warnings.filterwarnings(
'error', r"DateTimeField received a naive datetime",
RuntimeWarning, r'django\.db\.models\.fields')
@sebastibe
sebastibe / module_pattern.js
Created January 9, 2013 05:25
The pattern known as module of Addy Osmani.
var Module = (function() {
var privateMember = "secret";
return {
publicMember: function() {
return privateMember;
}
};
})();
// Assuming we are working with the ASCII character set. 256 unique values.
//
// First, check that the length of the string is under the number of unique values.
// If it is greater, the string does not have all unique characters
//
// Second, create an array of boolean values of size of unique characters.
// Traverse the string and flag the array value from false to true when a character is hit.
// When the character is hit a second time, return false.
bool hasUniqueChars(char * string)
@sebastibe
sebastibe / conf.py
Last active December 10, 2015 14:50
Sphinx conf.py for the documentation of a Django project.
import sys
from os.path import abspath, join, dirname
DOC_ROOT = abspath(dirname(dirname(__file__)))
BASE_PATH = dirname(DOC_ROOT)
PROJECT_PATH = join(BASE_PATH, 'api')
sys.path.append(BASE_PATH)
sys.path.append(PROJECT_PATH)