Skip to content

Instantly share code, notes, and snippets.

@quandyfactory
quandyfactory / fizzbuzz.py
Created April 28, 2010 18:46
fizzbuzz solution in 5 lines of code (plus 1 line docstring).
def fizzbuzz(start=1, end=100, fizzval=3, buzzval=5):
"""Implements Reginald Braithwaite's FizzBuzz text."""
for i in range(start, end+1):
fizz = 'Fizz' if i % fizzval == 0 else ''
buzz = 'Buzz' if i % buzzval == 0 else ''
print i if fizz+buzz == '' else fizz+buzz
<textarea style="width: 90%; height: 90%; display: block; margin: 2% auto auto auto;"></textarea>
<!-- open this in a modern browser and you have built-in spell checking with no other clutter -->
/*
Stupid, stupid way of enabling second-tier popout menus on IE6 but what the hell.
Yes, this can be done with about 3 lines of CSS in a browser that's not "special".
All I can say is, THANK YOU jQuery!
*/
if ($.browser.msie == true && $.browser.version == 6) {
$('ul#menu li').mouseover( function() {
var html = $(this).html();
if (html.indexOf('<div>') > -1 || html.indexOf('<DIV>') > -1) {
@quandyfactory
quandyfactory / make_table.py
Created June 23, 2010 15:40
Makes an HTML table.
def make_table(data, delim='\t', table_id='make_table', classname=''):
"""
Converts delimited data into an HTML table.
- `data` is a string, with rows delimited by newlines.
- Default cell delimiter for each row is a tab.
"""
output = []
output.append('<table id="%s" class="%s">' % (table_id, classname))
for row in data.strip().split('\n'):
cells = row.strip().split(delim)
@quandyfactory
quandyfactory / rps.py
Created July 1, 2010 04:24
Simple command-line Rock, Paper, Scissors game.
#!/usr/bin/env python
"""
Simple command-line based Rock Paper Scissors game in Python.
"""
import random
vals = 'R P S'.split(' ')
msg_win = "You win this round."
msg_lose = "You lose this round."
msg_tie = "Tie! You both picked "
@quandyfactory
quandyfactory / parse_candidates.py
Created September 10, 2010 12:18
Turns a blob from the city of #HamOt's election page into a list of dictionaries.
def prepare(blob):
"""
Cleans up the City of Hamilton's blob of text from its candidates page and turns it into a list of dictionaries.
Here's the URL: http://www.hamilton.ca/CityDepartments/CorporateServices/Clerks/MunicipalElection/Nominated+Candidates.htm
"""
blobs = blob.split('\n# ')
wards = []
for blob in blobs:
title = blob.split('\n')[0]
@quandyfactory
quandyfactory / candidates.py
Created September 11, 2010 02:47
Turns the City of #HamOnt's election page blog into SQL insert queries.
#!/usr/bin/env python
def prepare(blob):
"""
Cleans up the City of Hamilton's blob of text from its candidates page and turns it into a list of dictionaries.
Here's the URL: http://www.hamilton.ca/CityDepartments/CorporateServices/Clerks/MunicipalElection/Nominated+Candidates.htm
We grabbed the source code for the candidates, minus the rest of the HTML boilerplate from the page, and added an octothorpe # before each ward heading to make the ward sections easier to parse.
"""
blobs = blob.split('\n# ')
@quandyfactory
quandyfactory / get_google_rank_for_candidates.py
Created October 5, 2010 14:38
Checks a list of names to determine whether they are in the top four Google search results.
import json
import urllib
import urllib2
candidates = ['David Sweet', 'James W. Byron', 'Brad Clark', 'Terry Anderson', 'Stephen E. Brotherston', 'Dave Braden', 'Annie Tennier', 'Michelle Stockwell', 'Marie Bountrogianni', 'Nancy MacBain', 'David Christopherson', 'Wayne Marston', 'Chris Charlton', 'Peter Ormond', 'Karen Burson', 'David Hart Dyke', 'Dean Allison', 'Stephen Henry Bieda', 'David Heatley', 'Bryan Jongbloed', 'Jim Enos', 'Michael James Baldasaro', 'Jamile Ghaddar', 'Anthony Giles', 'Lisa Nussey', 'Wendell Fields', 'Bob Green Innes', 'Bob Mann', 'Greg Pattinson', 'Henryk Adamiec', 'Sid Frere', 'Gord Hill']
url_template = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={{q}}&key={{key}}&userip={{userip}}'
key = 'YOUR-GOOGLE-API-KEY'
referer = 'http://your.domain.com'
userip = '127.0.0.1'
@quandyfactory
quandyfactory / RTH_Elections_API_example.php
Created October 9, 2010 03:17
PHP example using the RTH Elections API.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>RTH Elections Site: Using JSON in PHP</title>
</head>
<body>
<h1>RTH Elections Site: Using JSON in PHP</h1>
<?php
$url = 'http://elections.raisethehammer.org/api/election/1';
@quandyfactory
quandyfactory / hamont_election_results.py
Created October 27, 2010 16:20
Nasty hack to get city of #HamOnt election data into a usable format.
#!/usr/bin/env python
"""
Grabs election data from the City of Hamilton's GEMS election data pages, converts it into a json object and an sql insert statement.
Yeah, I know: the code is seriously ugly-ass and inelegant. What the hell, it works.
Update: The City changed their election results pages and this script no longer works. I'm working on updating it.
Very Important Note: this code breaks on ward 14, where Rob Pasuta is acclaimed and there are no ward results on the web page.
Instead of fixing it in the code, for now I just fixed the SQL statement manually by removing the trustees from the results.