This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jingo | |
from django_assets.env import get_env | |
jingo.env.assets_environment = get_env() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Alt 1: very explicit, but requires knowledge on what type of workers exist | |
from rq.workers.gevent import GeventWorker | |
w = GeventWorker(4) | |
# Alt 2: A bit like an "Abstract Factory", no need for knowing the implementing | |
# class names, backward compatible, and kind='forking' by default | |
from rq import Worker | |
w = Worker(4, kind='gevent') | |
# Alt 3: I don't think (2) works without really nasty tricks. This one is as |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Theres a gevent pool of 4 greenlets. Jobs are spawned continuously, blocking | |
until a free greenlet is available (default gevent.pool behaviour). Say, | |
a unit of work takes rougly a second. Then, at any moment, there are likely | |
4 jobs mid-execution. | |
The Goal | |
-------- | |
I want to catch both SIGINT/KeyboardInterrupt _and_ SIGTERM in | |
the main loop, and in a nondestructive way. By nondestructive, I mean (1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |