Skip to content

Instantly share code, notes, and snippets.

@quandyfactory
quandyfactory / insult.py
Created December 17, 2009 18:12
Generates an Elizabethan insult.
#!/usr/bin/env python
__version__ = '0.2'
__releasedate__ = '2009-12-23'
__author__ = 'Ryan McGreal <ryan@quandyfactory.com>'
__homepage__ = 'http://quandyfactory.com/insult'
__repository__ = 'http://gist.github.com/258915'
__copyright__ = 'Copyright (C) 2009 by Ryan McGreal. Licenced under GPL version 2. http://www.gnu.org/licenses/gpl-2.0.html'
def generate_insult():
@quandyfactory
quandyfactory / longurl.py
Created December 22, 2009 21:24
Code to run Wayne MacPhail's URL Lengthener.
def get_longurl(path):
"""
Code to run Wayne MacPhail's URL Lengthener.
TODO: re-populate form fields on failed post.
"""
output = []
addline = output.append
formfields = web.input(longurl='', url='')
longurl = tools.strip_html(formfields.longurl.strip().lower().replace('http://', '').replace('/','_'))
url = tools.strip_html(formfields.url.strip().lower())
@quandyfactory
quandyfactory / iterchars.py
Created January 7, 2010 15:31
Creates an iterator of ascii characters from 32 (space) to 126 (tilde)
def iterchars():
"""
Creates an iterator of ascii characters from 32 (space) to 126 (tilde)
"""
for x in range(32, 127):
yield x
@quandyfactory
quandyfactory / hasmethods.py
Created February 4, 2010 15:53 — forked from vlazzle/hasmethods.py
hasmethods function.
# if there are any others who don't care much for try ... catch AttributeError
def hasmethods(obj, *meth_names):
return all(
hasattr(
# if it calls like a method it's a method
getattr(obj, m, None),
'__call__'
) for m in meth_names
)
@quandyfactory
quandyfactory / anagram.py
Created April 14, 2010 16:46
Determine whether two strings are anagrams.
def anagram(string1, string2, ignore_whitespace=False):
"""Determines whether two strings are anagrams."""
if ignore_whitespace==True:
import re
string1, string2 = re.sub('\s', '', string1), re.sub('\s', '', string2)
if len(string1) != len(string2): return False
list1, list2 = [c for c in string1].sort(), [c for c in string2].sort()
if list1 != list2: return False
return True
@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 "