Skip to content

Instantly share code, notes, and snippets.

View davidbgk's full-sized avatar
Let’s escape GAFAM+ when/while we can!

David Larlet davidbgk

Let’s escape GAFAM+ when/while we can!
View GitHub Profile
davidbgk / A granular permissions' backend for Django
Created March 31, 2010 13:43
A granular permissions' backend for Django
View A granular permissions' backend for Django
# A granular permissions' backend for Django
except NameError:
from sets import Set as set # Python 2.3 fallback
from django.contrib.auth.backends import ModelBackend
class PermissionsBackend(ModelBackend):
davidbgk / Wipe your tweets
Created April 15, 2010 07:59
Delete your old tweets
View Wipe your tweets
Delete your old tweets (by old I mean all except latest 100)
without removing favorites of your own tweets and DM.
1/ Install python-twitter: as ptwitter
2/ Patch it with
3/ Populates USERNAME and PASSWORD
Note that you're limited to 150 calls to the API per hour and that you can't
retrieve more than the 3200 latest tweets, it means that even if you relaunch
davidbgk /
Created April 28, 2010 16:20
Django: Current Page Middleware
Current Page Middleware.
This module provides a middleware that implements a mechanism to
highlight a link pointing to the current URL.
Thanks @davidbgk and @samueladam for improvements & optimizations
import re
davidbgk / classinstancemethod
Created April 30, 2010 07:21 — forked from ianb/Lightning talk
when you want to use both a classmethod and the function with a class instance
View classinstancemethod
# classinstancemethod, when you want to use both a classmethod and the function with a class instance
## Descriptors
## Anything with __get__ and optionally __set__:
class classinstancemethod(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, type=None):
davidbgk / To load Django fixtures without any IntegrityError
Created June 28, 2010 13:50
To load Django fixtures without any IntegrityError
View To load Django fixtures without any IntegrityError
from django.db.models import signals
from django.contrib.auth import models as auth_app
from import create_permissions, create_superuser
from import update_contenttypes
dispatch_uid = "")
signals.post_syncdb.disconnect(create_superuser ,
sender=auth_app, dispatch_uid = "")
davidbgk /
Created October 28, 2010 10:30
Easily add an empty choice to a Django ChoiceField
from django import forms
class EmptyChoiceField(forms.ChoiceField):
def __init__(self, choices=(), empty_label=None, required=True, widget=None, label=None,
initial=None, help_text=None, *args, **kwargs):
# prepend an empty label if it exists (and field is not required!)
if not required and empty_label is not None:
choices = tuple([(u'', empty_label)] + list(choices))
davidbgk / gist:805600
Created February 1, 2011 08:46 — forked from adamcharnock/gist:389875
Double metaphone
View gist:805600
import re
from metaphone import dm as double_metaphone
# get the Redis connection
from jellybean.core import redis
import models
# Words which should not be indexed
#coding= utf-8
# This script implements the Double Metaphone algorithm (c) 1998, 1999 by Lawrence Philips
# it was translated to Python from the C source written by Kevin Atkinson (
# By Andrew Collins - January 12, 2007 who claims no rights to this work
# Tested with Pyhon 2.4.3
# Updated Feb 14, 2007 - Found a typo in the 'gh' section
# Updated Dec 17, 2007 - Bugs fixed in 'S', 'Z', and 'J' sections. Thanks Chris Leong!
# Updated June 25, 2010 - several bugs fixed thanks to Nils Johnsson for a spectacular
davidbgk /
Created July 26, 2011 13:24 — forked from jsocol/
Give an image, get a data-uri
#!/usr/bin/env python
"""Command line script to convert a file, usually an image, into a data URI
for use on the web."""
import base64
import mimetypes
import os
import sys
davidbgk /
Created October 25, 2011 01:37
A very simple HTTP server in Python using wsgiref.simple_server
from cgi import parse_qs
from wsgiref.simple_server import make_server
def simple_app(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/plain')]
start_response(status, headers)
if environ['REQUEST_METHOD'] == 'POST':
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)