Skip to content

Instantly share code, notes, and snippets.

class QuerySetDoubleIteration(Exception):
"A QuerySet was iterated over twice, you probably want to list() it."
pass
# "Skinny" here means we use iterator by default, rather than
# ballooning in memory.
class SkinnyManager(Manager):
def get_query_set(self):
return SkinnyQuerySet(self.model, using=self._db)
@zahardzhan
zahardzhan / tictactoe.py
Created August 30, 2010 14:00
TicTacToe
def wins(grid):
rows = [[grid[i+j] for j in 0, 1, 2] for i in 0, 3, 6]
cols = [[grid[i+j] for j in 0, 3, 6] for i in 0, 1, 2]
digs = [[grid[i] for i in 0, 4, 8], [grid[i] for i in 2, 4, 6]]
return any(all(cell is 'x' for cell in row) or
all(cell is 'o' for cell in row)
for row in rows + cols + digs)
def full(grid):
return all(cell is 'x' or cell is 'o' for cell in grid)
<!DOCTYPE html>
<!-- Helpful things to keep in your <head/>
// Brian Blakely, 360i
// http://twitter.com/brianblakely/
-->
<head>
<!-- Disable automatic DNS prefetching.
@mislav
mislav / pagination.md
Created October 12, 2010 17:20
"Pagination 101" by Faruk Ateş

Pagination 101

Article by Faruk Ateş, [originally on KuraFire.net][original] which is currently down

One of the most commonly overlooked and under-refined elements of a website is its pagination controls. In many cases, these are treated as an afterthought. I rarely come across a website that has decent pagination, and it always makes me wonder why so few manage to get it right. After all, I'd say that pagination is pretty easy to get right. Alas, that doesn't seem the case, so after encouragement from Chris Messina on Flickr I decided to write my Pagination 101, hopefully it'll give you some clues as to what makes good pagination.

Before going into analyzing good and bad pagination, I want to explain just what I consider to be pagination: Pagination is any kind of control system that lets the user browse through pages of search results, archives, or any other kind of continued content. Search results are the o

@fcalderan
fcalderan / inception-javascript.js
Created November 2, 2010 09:42
inception explained as a 4 nested javascript closures
/*
* Fabrizio Calderan, twitter @fcalderan, 2010.11.02
* I had an idea: could Inception movie be explained by a few javascript closures
* and variable resolution scope (just for fun)?
*
* Activate javascript console =)
*/
<script>
console.group("inception movie");
@valpackett
valpackett / gist:666161
Created November 7, 2010 14:55
Coda-like swapping in Emacs
(defun coda-swap (expr)
(interactive "sExpr: ")
(query-replace-regexp
(replace-regexp-in-string "$[1-2]" "\\\\([0-9a-zA-Z]*\\\\)" expr)
(replace-regexp-in-string "$[1-2]" (lambda (m) (if (equal m "$1") "\\\\2" "\\\\1")) expr)))
;; Example:
; enter
; width="$1" height="$2"
; and it will replace
@levigross
levigross / djangoratelimit.py
Created November 29, 2010 02:05
Cache based rate limiting in Django
from django.core.cache import cache
from django.http import HttpResponseForbidden
from functools import wraps
from django.utils.decorators import available_attrs
def ratelimit(limit=10,length=86400):
""" The length is in seconds and defaults to a day"""
def decorator(func):
def inner(request, *args, **kwargs):
ip_hash = str(hash(request.META['REMOTE_ADDR']))
@MicahElliott
MicahElliott / colortrans.py
Created November 29, 2010 07:57
Convert values between RGB hex codes and xterm-256 color codes.
#! /usr/bin/env python
""" Convert values between RGB hex codes and xterm-256 color codes.
Nice long listing of all 256 colors and their codes. Useful for
developing console color themes, or even script output schemes.
Resources:
* http://en.wikipedia.org/wiki/8-bit_color
* http://en.wikipedia.org/wiki/ANSI_escape_code
@kanej
kanej / clj-to-ctags.awk
Created January 29, 2011 21:15
Awk program to create a ctags file from a clojure file. Use: >awk -f clj-to-ctags ./src/core/*.clj
/defn/ { print $2 "\t./" FILENAME "\t:" FNR }
@lucapette
lucapette / toys.rb
Created February 2, 2011 10:08
methods to create toys arrays and hashes
class Array
def self.toy(n=10, &block)
block_given? ? Array.new(n,&block) : Array.new(n) {|i| i+1}
end
end
class Hash
def self.toy(n=10)
Hash[Array.toy(n).zip(Array.toy(n){|c| (96+(c+1)).chr})]
end