This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def choose(s, choice=None): | |
if choice is None: | |
choice = random.choice | |
if len(s) == 0: | |
raise KeyError("Cannot choose from empty set") | |
return choice(list(s)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# System Modules | |
from collections import OrderedDict, defaultdict | |
from itertools import chain | |
import json | |
# External Modules | |
# Local Modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
while True: | |
user_input = raw_input("Enter 'q' to exit: ") | |
if user_input in ('q', 'Q'): | |
break | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# System Modules | |
import curses | |
import time | |
# External Modules | |
# Local Modules | |
def test(stdscr): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc -fpic --shared $(python-config --includes) greetmodule.c -o greetmodule.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Take a function and return a function that caches the results of | |
// that function. | |
// | |
// Setting debug to true will print the logic the caching function uses. | |
var memoize = function(fn, debug) { | |
// Caching object, in the form {args : value} | |
var cache = {}; | |
// Caching function. No explicit args. Any args are fed into the | |
// memoized function via apply(). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Simple class to track a player's life total and poison counts in | |
Magic: the Gathering. | |
Keeps a history of life total changes, and has some simple logic to | |
determine when a player has lost by on either life or poison counts-- | |
not accounting for cards that alter the life and poison rules, however. | |
These 'deaths' are not enforced, so a player may be healed up to 1 or | |
more life, or have the tenth poison counter removed and the class will | |
happily report that the player is Still Alive. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Prime generator via unbounded Sieve of Erathosthenes. | |
Ported from http://wthwdik.wordpress.com/2007/08/30/an-unbounded-sieve-of-eratosthenes/ | |
For each prime number we store in a vector a pair consisting of | |
the prime itself and a multiple, initialized to the prime number | |
again. We compare each candidate with each multiple; if the | |
multiple is smaller we increment it by the corresponding prime. | |
Then if the candidate is equal to the multiple we discard it as | |
it’s obviously a multiple of the current prime, otherwise we |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Limit calls to a function to a given rate. | |
Intended for use with functions that interact with systems not under the | |
programmers control (for example, downloading html content from servers | |
for parsing). | |
usage: | |
# Two rate limited functions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""A simple function (roughly) implementing C-style switch statements. | |
Usage:: | |
switch(value, { | |
'it': | |
"You said the word!", | |
'fruit': | |
"Suppose he's got a pointed stick.", | |