Skip to content

Instantly share code, notes, and snippets.

💭
(╯°□°)╯︵ ┻━┻

David Larlet davidbgk

💭
(╯°□°)╯︵ ┻━┻
Block or report user

Report or block davidbgk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@davidbgk
davidbgk / A granular permissions' backend for Django
Created Mar 31, 2010
A granular permissions' backend for Django
View 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 Apr 15, 2010
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: 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 Apr 28, 2010
Django: Current Page Middleware
View current_page_middleware.py
"""
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 Apr 30, 2010 — 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
davidbgk / To load Django fixtures without any IntegrityError
Created Jun 28, 2010
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 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 Oct 28, 2010
Easily add an empty choice to a Django ChoiceField
View fields.py
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 Feb 1, 2011 — forked from adamcharnock/gist:389875
Double metaphone
View gist:805600
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
View metaphone.py
#!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 Jul 26, 2011 — forked from jsocol/data-uri.py
Give an image, get a data-uri
View data-uri.py
#!/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 Oct 25, 2011
A very simple HTTP server in Python using wsgiref.simple_server
View server.py
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)
You can’t perform that action at this time.