Skip to content

Instantly share code, notes, and snippets.

View codeinthehole's full-sized avatar
🌏
The main difference between dinosaurs and us is we're building our own meteor.

David Winterbottom codeinthehole

🌏
The main difference between dinosaurs and us is we're building our own meteor.
View GitHub Profile
@codeinthehole
codeinthehole / SensibleTermarySniff.php
Created January 19, 2011 23:36
PHP_CodeSniffer sniff file for checking for idiotic ternary assignments
<?php
class TangentLabs_Sniffs_BestPractice_SensibleTernarysSniff implements PHP_CodeSniffer_Sniff
{
/**
* @return array
*/
public function register()
{
return array(T_INLINE_THEN);
}
@codeinthehole
codeinthehole / twill_testcase.py
Created June 1, 2011 11:49
Twill TestCase for django
class TwillTestCase(TestCase):
"""
Simple wrapper around Twill to make writing TestCases easier.
Commands availabel through self.command are:
- go -> visit a URL
- back -> back to previous URL
- reload -> reload URL
- follow -> follow a given link
- code -> assert the HTTP response code
@codeinthehole
codeinthehole / handler.py
Created June 2, 2011 10:55
Environment-based logger
from logging import FileHandler as BaseFileHandler
import os
class EnvFileHandler(BaseFileHandler):
"""
Custom filehandler that uses the LOG_ROOT setting to determine the folder
to store log files in.
We have to do some tricky stuff to avoid circular imports. To this end,
@codeinthehole
codeinthehole / curried_subclass.py
Created September 9, 2011 08:38
Lightweight subclassing using curry
def curry(f, *args, **kwargs):
def curried(*more_args, **more_kwargs):
return f(*(args+more_args), **dict(kwargs, **more_kwargs))
return curried
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
@codeinthehole
codeinthehole / release.sh
Created January 19, 2012 17:31
Simple script for releasing python packages to PyPi
#!/bin/bash
# Extract version number from setup.py
RELEASE_NUM=`grep version setup.py | cut -d\' -f2`
# Push to PyPi
python setup.py sdist upload
# Tag in Git and push to remote
git tag $RELEASE_NUM -m "Tagging release $RELEASE_NUM"
@codeinthehole
codeinthehole / poker.py
Created February 13, 2012 09:40
Determine how wins poker game from input file
#!/usr/bin/env python
import sys
from optparse import OptionParser
import unittest
from collections import defaultdict
# Constants for each hand
ROYAL_STRAIGHT_FLUSH = 10
STRAIGHT_FLUSH = 9
FOUR_OF_A_KIND = 8
@codeinthehole
codeinthehole / pre-commit
Created March 3, 2012 20:14 — forked from spulec/pre-commit
Yipit Pre-commit Hook
#!/usr/bin/env python
import os
import re
import subprocess
import sys
modified = re.compile('^(?:M|A)(\s+)(?P<name>.*)')
CHECKS = [
@codeinthehole
codeinthehole / fetch_pyvideo.py
Created March 11, 2012 22:36
Fetch PyCon videos from pyvideo.org and convert them to M4V so they can be synced to your iPhone
# Hacky script for downloading videos from PyVideo and converting them to m4v
# format so they can be synced onto your apple device. Useful if you
# want to see everything that happened at PyCon while commuting.
#
# Requirements:
# * pip install requests BeautifulSoup
# * youtube-dl (from https://github.com/rg3/youtube-dl/) - add this to the
# directory where this script runs and ensure its execute bit is set.
# This was the only YouTube downloader I found that worked. It doesn't
# really have a good Python API so I call it through os.system.
@codeinthehole
codeinthehole / url_api.py
Created March 16, 2012 12:02
Desired API for a python URL object
import requests
from someurllib import URL
# 1. Build request URL
url = URL(host='maps.google.com')
url.path('/geocoding/xml/')
.param('q', 'Some address string')
.param('sensor', 'False')
.fragment('something')
import random
import time
def pick_winners(candidates, num_winners):
for i in range(num_winners):
time.sleep(1)
winner = random.choice(candidates)
print "Winner %d: %s" % (i+1, winner)
candidates.remove(winner)
print "\nSorry %s\n" % ", ".join(candidates)