Skip to content

Instantly share code, notes, and snippets.

View martync's full-sized avatar

Martyn CLEMENT martync

View GitHub Profile
@martync
martync / show_in_browser.py
Created October 16, 2013 12:15
When testing a django application, sometime you just want to see what's happening on your html content. That does the trick.
import time
from datetime import datetime
from subprocess import call
def show_in_browser(response):
"""
Write the response content into a temporary HTML file and
open it into your default browser.
@martync
martync / aggregate_tags.py
Created April 29, 2013 09:10
Django aggregation in template filters.
from django import template
from django.db.models import Sum, Avg, Max, Min, Count
register = template.Library()
@register.filter
def sum(queryset, field):
return queryset.aggregate(sum_value=Sum(field)).get('sum_value')
@martync
martync / refresh_page.js
Created April 18, 2013 08:13
Ask the user to refresh the page after a countdown;
function refresh_after(seconds){
seconds--;
if (seconds<=0){
alert('The page has expired, click OK to reload.')
document.location.href = document.location.href;
}else{
setTimeout(refresh_after, 1000, [seconds])
}
}
@martync
martync / regroup_tag.py
Created March 26, 2013 14:36
Template tag for Django : Regroup list of lists by index, exactly like the core {% regroup %} tag.
from itertools import groupby
from django import template
from django.template.base import TemplateSyntaxError
register = template.Library()
class RegroupListNode(template.Node):
def __init__(self, target, expression, var_name):
def get_max_consecutive_number(datas):
"""
Retourne le nombre maximum de chiffres consécutifs
dans une listes. (Y'a t'il un built-in pour ça ? Itertool ?)
>> suit = [2003, 2008, 2007, 2001, 2010]
>> print get_max_consecutive_number(suit)
>>>> 2
>> suit = [2003, 2008, 2007, 2001, 2002, 2004]
from datetime import datetime
from os.path import abspath, dirname
import os
import json
phantom_script = """
var page = require('webpage').create();
page.open("%s", function (status) {
page.render("%s");
phantom.exit();
@martync
martync / ola.py
Created June 22, 2012 09:43 — forked from brunobord/ola.py
Olà in Python / ASCII art
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os
import time
def cls():
os.system(['clear', 'cls'][os.name == 'nt'])
figure_repos = '.o.'
@martync
martync / Render_to_XLS.py
Created February 9, 2012 14:40
Output an XLS file with XLWT
from django.template import Variable, defaultfilters
from django.http import HttpResponse
import xlwt
def render_to_xls(queryset, filename, fields):
"""
Output an XLS file of the queryset
Usage :
-------