Skip to content

Instantly share code, notes, and snippets.

View Ceasar's full-sized avatar

Ceasar Ceasar

View GitHub Profile
@Ceasar
Ceasar / views.py
Created October 29, 2011 06:39
The department view.
def deptartment(request, dept):
dept = dept.upper()
department = Departent(pcr('depts', dept))
context = {'courses': [course for course in department.courses]}
return render_to_response('department.html', context)
"""A collection of wrappers for API objects in the PennCourseReview API."""
from api import pcr
class CourseHistory(object):
"""A wrapper for coursehistory objects in the PCR API."""
def __init__(self, raw_coursehistory):
self.raw = raw_coursehistory
self.id = raw_coursehistory['id']
self.name = raw_coursehistory['name']
self.aliases = raw_coursehistory['aliases']
@Ceasar
Ceasar / pre-commit
Created November 29, 2011 07:27 — forked from steder/pre-commit.py
Git Pre-commit Hook For Python now with extra Python!
#!/usr/bin/env python
#-*- mode: python -*-
from subprocess import Popen, PIPE
import sys
syntax_checker = "pyflakes"
def run(command):
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
@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:
@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 / 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 / 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 / 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 / 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 / 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):