Skip to content

Instantly share code, notes, and snippets.

View bmispelon's full-sized avatar

Baptiste Mispelon bmispelon

View GitHub Profile
@bmispelon
bmispelon / custom_user_forms.py
Created November 24, 2012 12:50
Test for django ticket #19353
from django.contrib.auth.tests.custom_user import ExtensionUser
from django.contrib.auth.forms import UserCreationForm
from django.test import TestCase
from django.test.utils import override_settings
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = ExtensionUser
@bmispelon
bmispelon / bug.py
Created December 4, 2012 09:58
potential django bug
# found by frequant on #django
class BaseRatingModel(models.Model):
"""
Base rating model
"""
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
user = models.ForeignKey(settings.AUTH_USER_MODEL)
@bmispelon
bmispelon / recipe.txt
Created December 16, 2012 16:39
Cookies
Ingredients (~50 cookies):
* 200g flour
* 100g sugar
* 100g butter
* 1 egg
* baking powder
* Various fillings (chocolate chips, dried fruits, nuts, caramel, ...)
Mix egg+sugar. Add flour+baking powder. Add melted butter. Add fillings.
@bmispelon
bmispelon / balettcipo.py
Created December 19, 2012 16:03
Daily menus for restaurants around the office
# http://balettcipo.hu/hu/napi-menu
from lxml import html
from textwrap import dedent
import sys
from base import get_weekday
URL = 'http://balettcipo.hu/hu/napi-menu'
@bmispelon
bmispelon / example.py
Created January 14, 2013 15:31
A form/view combo to create linked objects
# models.py
from django.db import models
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo)
import re
from django import template
register = template.Library()
UNICODE_ESCAPE_SEQ = re.compile(r'\\x([0-9a-f]{2})
@register.filter
@bmispelon
bmispelon / ssn_fr.py
Created February 14, 2013 13:55
French social security number validation
from collections import namedtuple
FrenchSSNTuple = namedtuple('FrenchSSNTuple', [
'sex',
'year',
'month',
'departement',
'city',
'country',
'id_month_city',
@bmispelon
bmispelon / hack.py
Last active December 14, 2015 01:29
Get a list of identifiers in a string.Template()
def get_identifiers(tpl):
identifiers = []
for match in tpl.pattern.finditer(tpl.template):
d = match.groupdict()
identifier = d['named'] or d['braced']
if identifier:
identifiers.append(identifier)
return identifiers
@bmispelon
bmispelon / collisions.py
Created February 22, 2013 08:08
Looking for dict collisions
import itertools
from string import ascii_letters
import sys
def collisions(alphabet=ascii_letters, n=6):
d = {}
for i in range(2, n):
for comb in itertools.combinations_with_replacement(alphabet, i):
w = ''.join(comb)
@bmispelon
bmispelon / bug.py
Created February 22, 2013 11:45
Bug in django's forloop template compiling
from django.template import Template, Context
CONTEXT = Context({
's': '',
})
for tpl in [
'{% for x in s|add:"a b c" %}-{{ x }}{% endfor %}', # Doesn't work
'{% with v=s|add:"a b c" %}{% for x in v %}-{{ x }}{% endfor %}{% endwith %}',
'{% with v="a b c" %}{% for x in s|add:v %}-{{ x }}{% endfor %}{% endwith %}'