Skip to content

Instantly share code, notes, and snippets.

View larsyencken's full-sized avatar

Lars Yencken larsyencken

View GitHub Profile
@larsyencken
larsyencken / human_time.py
Created September 23, 2011 03:53
Pretty print time in seconds
def human_time(s):
"""
Given a time interval in seconds, return it pretty-printed.
>>> human_time(3.234)
'3.2s'
>>> human_time(91)
'1m 31s'
>>> human_time(3691)
'1h 1m'
@larsyencken
larsyencken / human_size.py
Created September 23, 2011 03:58
Pretty prints a size in bytes
def human_size(s):
"""
Given a size interval in seconds, return it pretty-printed.
>>> human_size(875)
'875b'
>>> human_size(3037)
'3.0Kb'
>>> human_size(3691234)
'3.7Mb'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# take.py
# bin
#
# Created by Lars Yencken on 2011-09-30.
# Copyright 2011 Lars Yencken. All rights reserved.
#
@larsyencken
larsyencken / flakey_server
Created October 11, 2011 23:47
A web server which fails according to known probability
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# flakey_server.py
#
# Created by Lars Yencken on 2011-10-12.
#
"""
A web server which fails randomly and occasionally.
#!/bin/bash
#
# highlight
#
# Highlight matching lines in the input. Like a less aggressive grep.
#
RED=$(echo -e '\033[31m')
NORMAL=$(echo -e '\033[00m')
@larsyencken
larsyencken / a_star.py
Created November 4, 2011 21:48
Pathfinding for the ants problem
def a_star_search(loc, dest, ants):
def cost_f(path):
return len(path) + ants.distance(path[-1], dest)
initial_path = (loc,)
frontier = [(cost_f(initial_path), initial_path)]
while frontier:
cost, path = frontier.pop()
@larsyencken
larsyencken / status_codes.txt
Created December 6, 2011 01:04
Valid HTTP status codes
100
101
200
201
202
203
204
205
206
300
#
# stopwords.txt
#
# Freely available stopword list, balancing coverage and size.
#
# From http://www.lextek.com/manuals/onix/stopwords1.html
a
about
above
across
@larsyencken
larsyencken / distribution_example.sh
Created January 6, 2012 21:23
Generating a static frequency distribution in shell
cat examples | sort | uniq -c | sort -nr | head -n 50
@larsyencken
larsyencken / rabbit_hole.py
Created June 21, 2012 02:10
Infinitely nested lazy dictionary
from collections import defaultdict
def rabbit_hole_dict():
"Infinitely nested lazy dictionary."
return defaultdict(rabbit_hole_dict)