Skip to content

Instantly share code, notes, and snippets.

View dokterbob's full-sized avatar

Mathijs de Bruin dokterbob

View GitHub Profile
@dokterbob
dokterbob / encode_web.sh
Created May 10, 2013 12:28
Script for encoding web video with FFMPEG.
# This script transcodes a single MP4 video to WebM and Theora and
# creates a still for the video after 2 seconds
# Inspiration: http://johndyer.name/ffmpeg-settings-for-html5-codecs-h264mp4-theoraogg-vp8webm/
INFILE=$1
FORMAT=$2
BASENAME="${INFILE%.*}"
VIDEO_BITRATE="1500k"
@dokterbob
dokterbob / filters.py
Created August 16, 2012 09:30
Generic filtering and counting
# Counting the amount of projects by distinct tag in a queryset
Project.objects.values('tags__name').annotate(projects=Count('id')).order_by()
"""
For filtering, this leads to the following design pattern:
1. Given a set of filters, apply each filter except for the current one.
2. Run `qs.values(<filtered_field>).annotate(projects=Count('id')).order_by()` to determine available options and counts.
3. Return to the client (either through AJAX or through a form) and render appropriately.
@dokterbob
dokterbob / .gitignore
Created July 19, 2012 08:58
django-nose issue testcase
*.o
*.pyc
.DS\_Store
build
@dokterbob
dokterbob / gist:3136197
Created July 18, 2012 13:25
Guess filename extension
"""
Open files without extenions and guess mimetype through python-magic.
"""
# Fix extensions
(root, ext) = path.splitext(full_path)
if ext == '':
import magic, mimetype
@dokterbob
dokterbob / models.py
Created June 21, 2012 14:45
Example of loosely coupling Django apps using settings.
from django.db import models
# Import the PROJECT_MODEL string from the current app's settings
from .settings import PROJECT_MODEL
class Task(models.Model):
"""
Instead of explicitly giving the model for the FK as a Python object or
explicitly stating a string in the form '<app>.<modelname>', we'll use
@dokterbob
dokterbob / snippet.py
Created March 7, 2012 14:50
Default validator
from django.core.exceptions import ValidationError
...
def default_validator(value):
""" Make sure only one instance of MyModel has default=True """
if value and MyModel.objects.filter(default=True).exists():
raise ValidationError('A default has already been defined.')
@dokterbob
dokterbob / compare.py
Created February 17, 2012 13:56
Compare versions Python PIP requirements
oldfile = open('freeze_old.txt')
newfile = open('freeze_new.txt')
packages = {}
def splitpackage(line):
result = line.split('==')
if not len(result) == 2:
result = line.split('@')
def sanitize_log_data(secret, data=None, leave_characters=4):
"""
Clean private/secret data from log statements and other data.
Assumes data and secret are strings. Replaces all but the first
`leave_characters` of `secret`, as found in `data`, with '*'.
If no data is given, all but the first `leave_characters` of secret
are simply replaced and returned.
"""
@dokterbob
dokterbob / validators.py
Created August 31, 2011 15:03
Validator for files, checking the size, extension and mimetype.
from os.path import splitext
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
class FileValidator(object):
"""
Validator for files, checking the size, extension and mimetype.
@dokterbob
dokterbob / next_previous.py
Created June 2, 2011 10:19
Django template tag to efficiently get the next or previous object in the Django queryset, with regards to the item specified.
from django import template
register = template.Library()
from templatetag_sugar.register import tag
from templatetag_sugar.parser import Constant, Variable, Name
from .utils import get_next_or_previous
"""
Efficient and generic get next/previous tags for the Django template language,