Skip to content

Instantly share code, notes, and snippets.

View nvie's full-sized avatar
🍀
Simplifying things

Vincent Driessen nvie

🍀
Simplifying things
View GitHub Profile
@nvie
nvie / b.py
Last active March 7, 2017 13:48
# Define types of building blocks
SUBDOMAIN = STRING.set('validate', validate_subdomain)
...
@schema({
'data': NESTED({
'type': WEBSPACE_TYPE,
'attributes': NESTED({
'subdomain': SUBDOMAIN,
@nvie
nvie / ormtools.py
Created November 29, 2013 08:59
Really useful helper that I use constantly to force myself to write more efficient Django queries.
from contextlib import contextmanager
from django.conf import settings
from django.db import connection
@contextmanager
def no_queries_allowed():
"""This is a helper method that makes it easier during development, by
throwing an exception when any queries are made within its block. Using
@nvie
nvie / sift.py
Last active December 18, 2015 15:28
The sift itertool.
from collections import deque
from functools import wraps
def inversify(predicate):
"""Returns a predicate that is the inverses of the given predicate."""
@wraps(predicate)
def _inner(*args, **kwargs):
return not predicate(*args, **kwargs)
return _inner
@nvie
nvie / where_to_put_this_best.py
Created March 7, 2013 20:55
What's the best place to put this code? This is basically setup code that you want to execute on Django's boot time. I'm currently putting it in an arbitrary app's __init__.py, but that feels a bit lame. Ideas? Settings seems like a bad idea, too.
import jingo
from django_assets.env import get_env
jingo.env.assets_environment = get_env()
@nvie
nvie / log.txt
Created March 4, 2013 16:24
The following code crashes when ran on Django==1.5, but it works fine on Django==1.4.3. (Both py27.)
$ python minimal_example.py
Traceback (most recent call last):
File "minimal_example.py", line 7, in <module>
s.sign('foo')
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/core/signing.py", line 175, in sign
return str('%s%s%s') % (value, self.sep, self.signature(value))
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/core/signing.py", line 169, in signature
signature = base64_hmac(self.salt + 'signer', value, self.key)
File "/Users/nvie/.virtualenvs/foo/lib/python2.7/site-packages/django/core/signing.py", line 75, in base64_hmac
return b64_encode(salted_hmac(salt, value, key).digest())
@nvie
nvie / gist:5082554
Created March 4, 2013 14:25
Broken call to set_signed_cookie() since upgrading to Django 1.5.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)
Stacktrace (most recent call last):
File "connect/helpers.py", line 47, in get
resp.set_signed_cookie('foo', 'bar', max_age=600)
File "django/http/response.py", line 188, in set_signed_cookie
value = signing.get_cookie_signer(salt=key + salt).sign(value)
File "django/core/signing.py", line 91, in get_cookie_signer
return Signer('django.http.cookies' + settings.SECRET_KEY, salt=salt)
@nvie
nvie / inve
Created March 1, 2013 11:26
Use in place of `workon`. Put it in ~/bin.
#!/bin/sh
ENV="$1"
shift 1
VIRTUAL_ENV="${WORKON_HOME}/${ENV}"
if [ ! -f "${VIRTUAL_ENV}/bin/python" ]; then
echo "No virtualenv named '$ENV'."
exit 2
fi
@nvie
nvie / p.fish
Created February 13, 2013 09:43
This is my favorite way of automatically invoking the best suitable Python REPL. It is smart about the environment that it is invoked in (e.g. will respect your current virtual env) and is smart about which interpreter fits best (e.g. using `ipython` if available, or using `python manage.py shell` in case of Django, or `python manage.py shell_pl…
function p --description 'Start the best Python shell that is available'
set -l cmd
if test -f manage.py
if pip freeze ^/dev/null | grep -q 'django-extensions'
set cmd (which python) manage.py shell_plus
else
set cmd (which python) manage.py shell
end
else
@nvie
nvie / huh.md
Last active October 21, 2015 15:25

Here's my config:

$ cat Procfile.test
foo: python -u foo.py

$ cat foo.py
import logging
logging.basicConfig()
logging.warn('A log line\nWith a line break')

logging.warn('Second log')

@nvie
nvie / git-cleanup.sh
Created December 3, 2012 10:49
Very rough script that cleans up all old branches that are loosely hanging around after a while. Also updates the origin remote.
#!/bin/sh
fix_branch_output () {
# Strips first columns from the output, to remove whitespace and "current
# branch" marker from git-branch's output
cut -c 3-
}
local_branches () {
git branch | fix_branch_output