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
davidbgk / A granular permissions' backend for Django
Created March 31, 2010 13:43
A granular permissions' backend for Django
# A granular permissions' backend for Django
try:
set
except NameError:
from sets import Set as set # Python 2.3 fallback
from django.contrib.auth.backends import ModelBackend
class PermissionsBackend(ModelBackend):
@davidbgk
davidbgk / Wipe your tweets
Created April 15, 2010 07:59
Delete your old 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: http://code.google.com/p/python-twitter/ as ptwitter
2/ Patch it with http://code.google.com/p/python-twitter/issues/detail?id=60
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
davidbgk / current_page_middleware.py
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
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
# 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
davidbgk / To load Django fixtures without any IntegrityError
Created June 28, 2010 13:50
To load Django fixtures without any IntegrityError
from django.db.models import signals
from django.contrib.auth import models as auth_app
from django.contrib.auth.management import create_permissions, create_superuser
from django.contrib.contenttypes.management import update_contenttypes
signals.post_syncdb.disconnect(update_contenttypes)
signals.post_syncdb.disconnect(create_permissions,
dispatch_uid = "django.contrib.auth.management.create_permissions")
signals.post_syncdb.disconnect(create_superuser ,
sender=auth_app, dispatch_uid = "django.contrib.auth.management.create_superuser")
@davidbgk
davidbgk / fields.py
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
davidbgk / gist:805600
Created February 1, 2011 08:46 — forked from adamcharnock/gist:389875
Double metaphone
import re
# http://atomboy.isa-geek.com/plone/Members/acoil/programing/double-metaphone
from metaphone import dm as double_metaphone
# get the Redis connection
from jellybean.core import redis
import models
# Words which should not be indexed
#!python
#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 (http://aspell.net/metaphone/)
# By Andrew Collins - January 12, 2007 who claims no rights to this work
# http://www.atomodo.com/code/double-metaphone
# 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
davidbgk / data-uri.py
Created July 26, 2011 13:24 — forked from jsocol/data-uri.py
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
davidbgk / server.py
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)