Skip to content

Instantly share code, notes, and snippets.

View dbrgn's full-sized avatar

Danilo Bargen dbrgn

View GitHub Profile
@dbrgn
dbrgn / queryset_generators.py
Created April 1, 2011 08:41
queryset_generator and queryset_list_generator
def queryset_generator(queryset, chunksize=1000):
"""
Iterate over a Django Queryset ordered by the primary key
This method loads a maximum of chunksize (default: 1000) rows in its
memory at the same time while django normally would load all rows in its
memory. Using the iterator() method only causes it to not preload all the
classes.
Note that the implementation of the generator does not support ordered query sets.
@dbrgn
dbrgn / timing.py
Created April 8, 2011 07:32
Time python functions
def time(f):
"""Wrapper function to time python callables."""
import datetime
d = datetime.datetime.today()
print 'Timing callable %s' % f.__name__
print '------------------'
f()
print '------------------'
print datetime.datetime.today() - d
@dbrgn
dbrgn / boyer-moore.py
Created August 18, 2011 12:55
Boyer-Moore-Algorithm in Python
class last_occurrence(object):
"""Last occurrence functor."""
def __init__(self, pattern, alphabet):
"""Generate a dictionary with the last occurrence of each alphabet
letter inside the pattern.
Note: This function uses str.rfind, which already is a pattern
matching algorithm. There are more 'basic' ways to generate this
dictionary."""
@dbrgn
dbrgn / Usage Example
Created September 7, 2011 13:48
A bash script to grep files in a specified directory and to replace values using sed
$ git status
# On branch master
nothing to commit (working directory clean)
$ find_replace.sh "TODO" "s/TODO/REMEMBER/g" apps/
Command: egrep -lrZ --binary-files=without-match "TODO" "/home/user/apps/" | xargs -0 -l sed -i -e "s/TODO/REMEMBER/g"
Really execute (y/n)? y
[danilo@dev5 db]$ git status
# On branch master
@dbrgn
dbrgn / fetch.py
Created December 20, 2011 14:20
Get list of films that most frequently use the word "fuck" and fetch the corresponding IMDB ratings.
# WARNING: Ugly hack.
import re
import requests
from BeautifulSoup import BeautifulSoup
import imdb
print 'Getting data from Wikipedia...'
@dbrgn
dbrgn / twtrack.py
Created April 6, 2012 13:01
A small Python script to track keywords on Twitter using the realtime streaming API.
Full repository -> https://github.com/gwrtheyrn/twtrack
@dbrgn
dbrgn / rst.video.py
Created June 13, 2012 07:56
ReStructuredText Youtube / Vimeo video embed directive
# -*- coding: utf-8 -*-
"""
ReST directive for embedding Youtube and Vimeo videos.
There are two directives added: ``youtube`` and ``vimeo``. The only
argument is the video id of the video to include.
Both directives have three optional arguments: ``height``, ``width``
and ``align``. Default height is 281 and default width is 500.
def get_free_port():
s = socket.socket()
s.bind(('', 0))
port = s.getsockname()[1]
s.close()
return port
from subprocess import Popen, PIPE, STDOUT
import time
def process_line(line):
print 'New line: "%s"' % line.strip('\n')
proc = Popen(['bash', 'print_lines.sh'], stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
while True:
line = proc.stdout.readline()
if line != '':
$(document).ready(function () {
$('.switch').each(function () {
var radio_yes = $(this).children('input[type=radio][value=1]')[0];
var toggle = $(this).children('label.switch-toggle');
if (radio_yes) {
// checkbox.addClass('hidden');
toggle.removeClass('hidden');