Skip to content

Instantly share code, notes, and snippets.

View bmispelon's full-sized avatar

Baptiste Mispelon bmispelon

View GitHub Profile
@bmispelon
bmispelon / gist:3082255
Created July 10, 2012 09:21
Playing with int(some_word, base=X)
#!/usr/bin/env python3
# 16078734
(lambda i,s,b,l: i(s(b()),l(s(...))<<i()**i()+i(b(i))))(int,str,bool,len)
@bmispelon
bmispelon / increment.py
Created July 10, 2012 12:42
Incrementing in python
lambda i: i++ (lambda j: j()**j())(type(i))
lambda i: (sum(range(x))*2)/x # decrement
lambda i: 2*i-(sum(range(i))*2)/i # banermatt
lambda i: __import__('functools').partial(i.__add__, 1)()
lambda i: i+(i is i) # SFJulie1 (kind of)
@bmispelon
bmispelon / bash_autocomplete.sh
Last active October 8, 2015 12:18
Rename subtitles files
function _mvsub_complete()
{
local word=${COMP_WORDS[COMP_CWORD]}
local filename subname
if [[ $COMP_CWORD = 1 ]]; then
# Only allow subtitles files (*.srt)
COMPREPLY=($(compgen -f -X "!*.srt" -- "${word}"))
else
# Only complete non-subtitles files that don't have a matching subtitle
COMPREPLY=()
@bmispelon
bmispelon / example_views.py
Created September 12, 2012 07:17
View examples
def create(request): # django.views.generic.CreateView
"""Create a new model object and save it to the database."""
if request.method == 'post':
form = ModelFormClass(request.POST)
if form.isvalid():
form.save()
return redirect(...)
else:
form = ModelFormClass()
return render('your_template.html', {'form': form})
@bmispelon
bmispelon / help.txt
Created September 19, 2012 14:02
Quick explanations of django's CBV
django.views.generic.base:
View:
The most basic class. All other class based views inherit from it.
To use it, create a method for each HTTP verb (get, post, put, ...).
These method get passed a request obejct as the first argument, as well
as whatever was captured in the url (*args, or **kwargs).
The request, the *args and **kwargs are stored on the instance.
To instanciate a view in your code, use `view_name = ClassBasedView.as_view()`.
@bmispelon
bmispelon / sumtag.py
Created September 26, 2012 13:11
A django template tag for summing sequences
from django import template
register = template.Library()
class SumNode(template.Node):
def __init__(self, sequence, loopvar, loopitem):
self.sequence = template.Variable(sequence)
self.loopvar = loopvar
self.loopitem = template.Variable(loopitem)
@bmispelon
bmispelon / offset_bug.py
Created October 9, 2012 08:50
Python offset bug
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
SYNTAXERROR_TEMPLATE = "%s = ())" # Unbalanced parentheses
def get_offset(varname):
"""Trigger a syntax error that uses the given variable name and return the
exception's offset, e.g. the position of the syntax error.
"""
@bmispelon
bmispelon / forms.py
Created October 17, 2012 06:41
Newsletter signup form
class NewsLetterSignupForm(forms.ModelForm):
class Meta:
model = NewsLetterSignup
fields = ['email']
def save(self, commit=True):
"""Return an existing instance if one exists with the same email.
Create and return it otherwise.
"""
@bmispelon
bmispelon / departements.json
Created November 14, 2012 08:13
Départements et régions de France
[
{
"pk": "01",
"model": "yourapp.yourmodel",
"fields": {
"region": 82,
"name": "Ain"
}
},
{
@bmispelon
bmispelon / forms.py
Created November 24, 2012 08:01
UserCreationForm issues with custom user model
from django.contrib.auth.forms import UserCreationForm
class SignupForm(UserCreationForm):
pass