Skip to content

Instantly share code, notes, and snippets.

View robertklep's full-sized avatar

Robert Klep robertklep

View GitHub Profile
@robertklep
robertklep / EventStreamMiddlewareWrapper.py
Created June 13, 2012 19:49
Wrapper function for Django middleware classes so they handle text/event-stream
# wrapper for middleware classes (I'm using this in combination with django-sse)
def EventStreamMiddlewareWrapper(original):
original_method = getattr(original, 'process_response', None)
if not original_method:
return original
def new_method(self, request, response):
# return immediately when the response is an event stream
if 'text/event-stream' in response.get('Content-Type', ''):
return response
@robertklep
robertklep / rfc822-to-datetime.py
Created June 14, 2012 05:46
Convert RFC822 timestamp string to datetime object in Python
from datetime import datetime
import email
# convert RFC822 timestamp string to datetime object
def rfc822(timestamp):
return datetime.fromtimestamp(email.Utils.mktime_tz(email.Utils.parsedate_tz(timestamp)))
if __name__ == '__main__':
import sys
@robertklep
robertklep / double-encoding-fixes.py
Created June 14, 2012 05:50
Functions to detect/fix double-encoded UTF-8 strings in Python
import re
# functions to detect/fix double-encoded UTF-8 strings
# Based on http://blogs.perl.org/users/chansen/2010/10/coping-with-double-encoded-utf-8.html
DOUBLE_ENCODED = re.compile("""
\xC3 (?: [\x82-\x9F] \xC2 [\x80-\xBF] # U+0080 - U+07FF
| \xA0 \xC2 [\xA0-\xBF] \xC2 [\x80-\xBF] # U+0800 - U+0FFF
| [\xA1-\xAC] \xC2 [\x80-\xBF] \xC2 [\x80-\xBF] # U+1000 - U+CFFF
| \xAD \xC2 [\x80-\x9F] \xC2 [\x80-\xBF] # U+D000 - U+D7FF
| [\xAE-\xAF] \xC2 [\x80-\xBF] \xC2 [\x80-\xBF] # U+E000 - U+FFFF
@robertklep
robertklep / strip-diacritics.py
Created June 14, 2012 05:51
Strip diacritics from Python string
import unicodedata
def remove_diacritics(s):
nkfd_form = unicodedata.normalize('NFKD', unicode(s))
return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])
@robertklep
robertklep / stopwords.py
Created June 14, 2012 05:53
Regular expression matching most common Dutch, German, English and Spanish stopwords
# -*- coding:utf-8 -*-
import re
STOPWORDS = re.compile(r'(?u)\b(?:a(?:(?:an|b(?:er|o(?:ut|ve))|fter|gain(?:st)?|l(?:(?:g(?:un(?:as|os)|o)|l(?:e[mnrs]?)?|so?|tijd))?|n(?:(?:d(?:er(?:(?:e[mnrs]?|[mnrs]))?)?|tes?|y))?|re|u(?:ch|[fs])|[mst]))?|b(?:e(?:(?:cause|en|fore|i(?:ng)?|low|tween|n))?|i(?:st?|[jn])|oth|ut|y)|c(?:o(?:mo|n(?:tra)?)|ua(?:ndo|l))|d(?:a(?:(?:ar|mit|nn?|s(?:selbe)?|zu|[tß]))?|e(?:(?:in(?:e[mnrs]?)?|m(?:selben)?|n(?:(?:selben|n))?|r(?:(?:er|selben?))?|s(?:(?:de|se(?:lben|n)))?|ze|l))?|i(?:ch|e(?:s(?:e(?:(?:lben?|[mnrs]))?)?)?|[drt])|o(?:(?:ch|e[ns]|ing|nde|or|rt|wn))?|u(?:(?:r(?:ante|ch|ing)|s))?)|e(?:(?:ach|ens?|in(?:(?:e[mnrs]?|ig(?:e[mnrs]?)?|mal))?|l(?:l(?:as?|os))?|n(?:tre)?|r(?:(?:a(?:(?:is|[ns]))?|es))?|s(?:(?:as?|os?|t(?:a(?:(?:ba(?:(?:is|[ns]))?|d(?:(?:as?|os?))?|mos|ndo|r(?:(?:emos|á[ns]?|é(?:is)?|ía(?:(?:is|mos|[ns]))?))?|s))?|e(?:mos)?|o[sy]?|uv(?:i(?:e(?:r(?:a(?:(?:is|[ns]))?|on)|se(?:(?:is|[ns]))?)|mos|ste(?:is)?|é(?:ramos|semos))|[eo])|á(?:(?:bamos|is|[ns]))?|é(?:(?:is|[ns]))?)|e
@robertklep
robertklep / softbreak.js
Created October 6, 2012 09:16
Allow a browser to soft break a string by inserting soft hyphens
String.prototype.softbreak = function(minimum_size) {
return this
.toString()
.replace(
new RegExp('(.{' + (minimum_size || 5) + '})', 'g'),
'$1­'
);
};
@robertklep
robertklep / gist:3844465
Created October 6, 2012 09:18
Create a humanized age string from a Javascript Date instance
Date.prototype.AGE_TABLE = [
[ 60, 'second' , 'seconds' ],
[ 3600, 'minute' , 'minutes' ],
[ 86400, 'hour' , 'hours' ],
[ 604800, 'day' , 'days' ],
[ 2628030, 'week' , 'weeks' ],
[ 31557600, 'month' , 'months' ],
[ 4294967295, 'year' , 'years' ]
];
@robertklep
robertklep / gist:3844473
Created October 6, 2012 09:19
Create a humanized size string from a Javascript Number instance
Number.prototype.sizify = function(precision) {
if (precision === undefined)
precision = 1;
if (this > 1200000000)
return (this / 1000000000.0).toFixed(precision) + ' G';
if (this > 1200000)
return (this / 1000000.0).toFixed(precision) + ' M';
@robertklep
robertklep / dabblet.css
Created November 29, 2012 11:03
Pure CSS "dubstep forbidden" sign...
/**
* Pure CSS "dubstep forbidden" sign...
*/
html, body {
margin : 0;
padding : 0;
width : 100%;
height : 100%;
font-family : sans-serif;
@robertklep
robertklep / dabblet.css
Created November 29, 2012 12:35
CSS3 dialog experiment
/**
* CSS3 dialog experiment
*/
html, body {
margin : 0;
padding : 0;
width : 100%;
height : 100%;
font-family : 'Montserrat Alternates';