Skip to content

Instantly share code, notes, and snippets.

@radiosilence
radiosilence / beautiful.sql
Last active August 14, 2016 11:16
Django Raw Annotations. I wanted to add some complex aggregates, but .extra(select=) adds your raw SQL to GROUP BY, which doesn't work for aggregates. So, I trick the annotate() function by creating my own class that pretends to be an aggregate, but really just dumps SQL. Do not use for user inputted data.
SELECT "businesses_business"."id", "businesses_business"."user_id", "businesses_business"."slug", "businesses_business"."name", "businesses_business"."business_type_id", "businesses_business"."street", "businesses_business"."additional", "businesses_business"."town", "businesses_business"."county", "businesses_business"."postcode_id", "businesses_business"."phone_number", "businesses_business"."fax_number", "businesses_business"."email", "businesses_business"."website", "businesses_business"."strapline", "businesses_business"."description", "businesses_business"."active", "businesses_business"."deleted", "businesses_business"."activation_code", "businesses_business"."created_on", "businesses_business"."account_confirmed", "businesses_business"."preview", "businesses_business"."paid", "businesses_business"."last_updated", "businesses_business"."copy_id", "businesses_business"."logo", "businesses_business"."security_reference", "businesses_business"."video", "businesses_business"."embed_url", "businesses_busine
@creaktive
creaktive / rainbarf.zsh
Last active April 10, 2025 09:50
rainbarf sans tmux (to enable, "source rainbarf.zsh", under zsh)
# abort if already under tmux
[[ -n $TMUX_PANE ]] && return
# zsh-specific includes
zmodload -i zsh/datetime
zmodload -i zsh/stat
# place to store the chart
RAINBARF_OUT=~/.rainbarf.out
# update period, in seconds
@thomseddon
thomseddon / gist:3511330
Last active March 8, 2023 03:39
AngularJS byte format filter
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});
@ndarville
ndarville / secret-key-gen.py
Created August 24, 2012 17:01
Generating a properly secure SECRET_KEY in Django
"""
Two things are wrong with Django's default `SECRET_KEY` system:
1. It is not random but pseudo-random
2. It saves and displays the SECRET_KEY in `settings.py`
This snippet
1. uses `SystemRandom()` instead to generate a random key
2. saves a local `secret.txt`
@sontek
sontek / snowjob.py
Created December 22, 2011 04:24
Make your terminal snow with python
#!/usr/bin/env python
import os
import random
import time
import platform
snowflakes = {}
try:
# Windows Support
@stefanv
stefanv / sparks.py
Created November 17, 2011 00:25
Command line sparks in Python
#!/usr/bin/python
# coding=utf-8
# Python version of Zach Holman's "spark"
# https://github.com/holman/spark
# by Stefan van der Walt <[email protected]>
"""
USAGE:
@strogonoff
strogonoff / middleware.py
Created November 16, 2011 08:56
Django middleware for cross-domain XHR. WARNING: Defaults are unsafe here. Make sure to set proper restrictions in production!
from django import http
try:
from django.conf import settings
XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS
XS_SHARING_ALLOWED_METHODS = settings.XS_SHARING_ALLOWED_METHODS
XS_SHARING_ALLOWED_HEADERS = settings.XS_SHARING_ALLOWED_HEADERS
XS_SHARING_ALLOWED_CREDENTIALS = settings.XS_SHARING_ALLOWED_CREDENTIALS
except AttributeError:
XS_SHARING_ALLOWED_ORIGINS = '*'
# These are my notes from the PragProg book on CoffeeScript of things that either
# aren't in the main CS language reference or I didn't pick them up there. I wrote
# them down before I forgot, and put it here for others but mainly as a reference for
# myself.
# assign arguments in constructor to properties of the same name:
class Thingie
constructor: (@name, @url) ->
# is the same as:
@davidminor
davidminor / partition-between.clj
Created January 7, 2011 17:14
Clojure - Split a collection into partitions on boundaries specified by supplied function.
(defn partition-between
"Splits coll into a lazy sequence of lists, with partition
boundaries between items where (f item1 item2) is true.
(partition-between = '(1 2 2 3 4 4 4 5)) =>
((1 2) (2 3 4) (4) (4 5))"
[f coll]
(lazy-seq
(when-let [s (seq coll)]
(let [fst (first s)]
(if-let [rest-seq (next s)]