Skip to content

Instantly share code, notes, and snippets.

View nvie's full-sized avatar
🍀
Simplifying things

Vincent Driessen nvie

🍀
Simplifying things
View GitHub Profile
@nvie
nvie / the_one_armed_frog.txt
Created May 26, 2012 18:34
The One Armed Frog (posted to thelistserve.com on May 26th 2012)
I have a tattoo of a frog on the top of my right foot. I drew him myself. When
people ask me why I always say that when I take a step, he hops.
But there’s a story behind that.
I was tree planting one year. While planting you need to move fast, the more
trees in the ground the more money you would make. I was never all that great
at it but I stuck with it and at the very least I kept going.
One rainy day, on a hill surrounded by tree stumps and torn up branches,
@nvie
nvie / gist:2784760
Created May 24, 2012 22:57
Functional composition
def composition(f, *rest):
"""Creates the functional composition for the given functions."""
if len(rest) == 0:
return f
def _comp(*args, **kwargs):
g = composition(*rest)
return g(f(*args, **kwargs))
return _comp
@nvie
nvie / gzip_middleware.py
Created May 22, 2012 15:11
A WSGI middleware wrapper to add gzip to your WSGI app
from gzip import GzipFile
from wsgiref.headers import Headers
import re
import cStringIO as StringIO
# Precompile the regex to check for gzip headers
re_accepts_gzip = re.compile(r'\bgzip\b')
# Precompile the regex to split a comma delimitered string of Vary headers
@nvie
nvie / gist:2225508
Created March 28, 2012 11:19
zsh-sourcing.txt
#
# .zshenv
# .zshenv is the 1st file zsh reads; it's read for every shell, even if
# started with -f (setopt NO_RCS)
# .zprofile
# read after zshenv, if the shell is a login shell
# .zshrc
# read after zprofile, if the shell is an interactive shell
# .zlogin
# read after zshrc, if the shell is a login shell
@nvie
nvie / timeouts.py
Created February 22, 2012 20:09
Example of a time-guarded context
import signal
class JobTimeoutException(Exception):
pass
class death_pentalty_after(object):
def __init__(self, timeout):
self._timeout = timeout
@nvie
nvie / gist:1757965
Created February 7, 2012 07:30
Infinite stdin and xargs
nvie@Turkish % cat numargs
#!/bin/sh
echo $#
nvie@Turkish % yes moo | xargs ./numargs
5000
5000
5000
5000
5000
5000
@nvie
nvie / gist:1440744
Created December 7, 2011 00:12
Sexy pixel generator for efficiently drawing ellipse (or circle) shapes, either outlined or filled.
def iter_ellipse_points(cx, cy, rx, ry, filled=False):
"""For details, refer to the John Kennedy paper on efficiently drawing the
pixels of an ellipse: http://homepage.smc.edu/kennedy_john/belipse.pdf
"""
assert rx > 0, 'Cannot compute ellipse points with zero radius.'
assert ry > 0, 'Cannot compute ellipse points with zero radius.'
# 2a2 and 2b2 are the 2*a^2 and 2*b^2 terms (constants)
_2a2, _2b2 = 2 * rx * rx, 2 * ry * ry
@nvie
nvie / TestFoo.m
Created March 29, 2011 19:18
Using an SQLite in-memory store for your tests
#import <SenTestingKit/SenTestingKit.h>
@implementation TestFoo
- (void)setUp
{
[super setUp];
// We use a in memory store instead of the real SQLite
// Store the model, coord and store in instance variables

StillMaintained status buttons

Nice status buttons you can add to your README file in your Github repositories to show your project’s current status, fetched from StillMaintained:

   

For a live demo, check out StillMaintained’s own README.

Code

@nvie
nvie / gist:727282
Created December 3, 2010 17:56 — forked from hmarr/gist:727195
from mongoengine import *
from mongoengine import DENY, CASCADE, NULLIFY, DO_NOTHING
from mongoengine import DeleteForbidden # or something equivalent
from mongoengine.queryset import QuerySet
class Post(Document):
title = StringField()
@classmethod