Skip to content

Instantly share code, notes, and snippets.

View Ceasar's full-sized avatar

Ceasar Ceasar

View GitHub Profile
@Ceasar
Ceasar / pre-commit
Created October 12, 2012 02:03
Git hook to make and run nosetests
#!/usr/bin/env python
#-*- mode: python -*-
import sh
sh.make()
sh.nosetests()
@Ceasar
Ceasar / ringbuffer.py
Created October 6, 2012 18:52
A Python implement of a ring-buffer. NOTE: Doesn't work (appendleft and append can override each other's value since resizing only appends to end)
class RingBuffer(object):
def __init__(self, items=None):
self._left = 0
self._right = 0
self._length = 0
self._items = []
if items is not None:
for item in items:
self.append(item)
@Ceasar
Ceasar / sitemap.py
Created August 12, 2012 22:32
Proof of concept code to solve the problem of how to build a sitemap or file system tree.
import os
from pprint import pprint
MAP = {}
CHILDREN_KEY = 'children'
for path, _, filenames in os.walk('./templates'):
path = path.split("/")[2:]
@Ceasar
Ceasar / bandit.py
Created August 11, 2012 18:41
Small script to simulate an n-armed bandit game.
"""Module for playing the n-arm bandit game."""
import random
class Arm(object):
"""A point generator."""
def __init__(self):
self.mean = random.normalvariate(0, 1)
def pull(self):
@Ceasar
Ceasar / colorize.py
Created August 11, 2012 18:40
Add colors to stdout when printing.
BLACK = 0
RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4
MAGENTA = 5
CYAN = 6
WHITE = 7
@Ceasar
Ceasar / big_animals.coffee
Created August 10, 2012 14:37
Does decorating a class method that uses super break it?
class Animal
constructor: (@name) ->
move: (meters) ->
alert @name + " moved #{meters}m."
class Snake extends Animal
move: ->
alert "Slithering..."
super 5
@Ceasar
Ceasar / retry.py
Created July 30, 2012 21:50
Retry a function until it succeeds or has failed a given number of times.
import functools
import time
ef retry(exceptions, tries=5, delay=0.25):
"""Retry the decorated function using an exponential backoff strategy.
If the function does not complete successfully after the specified number
of tries, the exception is raised normally."""
def wrapper(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
@Ceasar
Ceasar / floydwarshall.py
Created April 23, 2012 23:34
Simple implementation of floyd-warhsall algorithm in Python.
def adj(g):
"""
Convert a directed graph to an adjacency matrix.
Note: The distance from a node to itself is 0 and distance from a node to
an unconnected node is defined to be infinite.
>>> g = {1: {2: 3, 3: 8, 5: -4},
@Ceasar
Ceasar / urls.py
Created April 19, 2012 17:35
Example of how to use local static files in Django.
"""Based on https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development"""
from django.conf import settings
from django.conf.urls.defaults import include, patterns
from django.contrib import admin
from django.views.generic.list_detail import object_detail
from app.models import Place
@Ceasar
Ceasar / composer.py
Created April 18, 2012 23:05
Tool to generate music automatically using Markov Chains. Simply pass a Melopy mp file to the script and it will generate a song. Requires Nonsense and Melopy.
import sys
from itertools import izip
from melopy import Melopy
from nonsense import StationarySource
if __name__ == "__main__":
try:
sample = sys.argv[1] # must not have comments
except IndexError: