Skip to content

Instantly share code, notes, and snippets.

View bmispelon's full-sized avatar

Baptiste Mispelon bmispelon

View GitHub Profile
@bmispelon
bmispelon / django.contrib.auth.views.py
Created February 17, 2012 06:07
Django's auth using class-based views
# https://code.djangoproject.com/ticket/17209
import urlparse
from django.conf import settings
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponseRedirect, QueryDict
from django.utils.decorators import method_decorator
from django.utils.http import base36_to_int
from django.utils.translation import ugettext as _
from django.views import generic
@bmispelon
bmispelon / golf.py
Created April 27, 2012 15:08
A bit of golfing around empty sets
# 'set'
# can then be used with getattr(__builtins__, XXX)()
# or, even simpler, eval(XXX)()
''.join(help.__doc__[i] for i in [35, 47, 71])
str(bool())[-2:] + str(bool(' '))[0].lower()
(2 * '%s' % tuple(map(bool, range(2)))).lower()[3:6]
''.join(__import__('json').dumps(bool(x)) for x in range(2))[3:6] # not me
@bmispelon
bmispelon / gist:2699770
Created May 15, 2012 07:34
Playing around with context managers leaking into the global namespace
from contextlib import contextmanager
@contextmanager
def hacketyhack(val):
yield val
class Foo(object):
bar = 42
@bmispelon
bmispelon / weirdict.py
Created May 25, 2012 13:13
Making weird things with dicts
from collections import Mapping
from functools import wraps
def normalize_key(method):
"""A decorator for dict methods that take a key argument.
This decorator normalizes the key through the instance's keyfunc function.
"""
@wraps(method)
def wrapped(self, key, *args):
@bmispelon
bmispelon / binmatrix.py
Created May 29, 2012 17:11
Binary matrices
from collections import MutableMapping
class BinMatrix(MutableMapping):
def __init__(self, width, height):
self.width = width
self.height = height
self.ALIVE = 1
self.DEAD = 0
self._set = set()
@bmispelon
bmispelon / unformat.py
Created May 30, 2012 12:07
Reverse string formatting
import re
class conv(object):
@staticmethod
def int(s):
return int(s)
@staticmethod
def oct(s):
return int(s, base=8)
@bmispelon
bmispelon / TODO.txt
Created June 12, 2012 19:02
Put the zen back into python
* pass comand to add/remove admins
* pass command to join/leave a channel
@bmispelon
bmispelon / mandelbrot.py
Created June 21, 2012 14:31
Deobfuscating exercise
# http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python
from struct import pack
WIDTH, HEIGHT = 1500, 1000
f = open('M.bmp','wb')
f.write('BM' + pack('<QIIHHHH', WIDTH * HEIGHT * 3 + 26, 26, 12, WIDTH, HEIGHT, 1, 24))
@bmispelon
bmispelon / intfract.py
Created June 21, 2012 16:10
Useless math function.
def intfract(p, q, precision=10):
"""Return a tuple from the integer division p/q.
The first element is the integer part (p // q).
The second is a list of length `precision` containing the decimals.
Note: if the division is exact, the decimals list will not contain the
trailing 0s.
"""
intpart, rest = divmod(p, q)
@bmispelon
bmispelon / golf.py
Created July 4, 2012 13:54
Golfing
# Problem F at https://ep2012.europython.eu/pyddle
# 50 chars
import math;sum(map(int,str(math.factorial(1e3))))