This file contains 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
# Noughts and crosses for Python 2 | |
# http://www.reddit.com/r/learnpython/comments/oxgr0/converting_to_and_from_ternary/ | |
# Winning positions, represented as bit masks | |
wins = [ | |
'111 000 000', '000 111 000', '000 000 111', # Across | |
'100 100 100', '010 010 010', '001 001 001', # Down | |
'100 010 001', '001 010 100', # Diagonal | |
] | |
# Store winning boards as integers |
This file contains 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
# Comparison of LBYL versus EAFP | |
# http://www.reddit.com/r/Python/comments/pou99/python_performance_tips_part_1/ | |
from __future__ import print_function | |
from hashlib import md5 | |
_lbyl_cache = {} | |
_eafp_cache = {} | |
This file contains 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
# http://www.reddit.com/r/learnpython/comments/qkh43/new_to_python_searching_csv_files/ | |
# http://stackoverflow.com/questions/9564322/loop-through-rows-of-one-csv-file-to-find-corresponding-data-in-another | |
# http://stackoverflow.com/questions/9577997/search-through-csv-from-specific-row-down | |
import csv | |
# Difference constants. Note these are floats, so don't expect perfect decimal | |
# mathematics. | |
DELTA_HI = 0.001 |
This file contains 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
#!/usr/bin/env python | |
# http://www.reddit.com/r/learnpython/comments/qqnqt/metacritic_score_grabber/ | |
import urllib2 | |
import re | |
url = 'http://www.metacritic.com/game/xbox-360/mass-effect-3' | |
r = urllib2.urlopen(url) | |
html = r.read() |
This file contains 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
# Comparison of dictionary types | |
# http://www.reddit.com/r/Python/comments/qxjho/sdict_sorted_dictionary_for_python_sdict_is_a/ | |
from sdict import Dict | |
from collections import OrderedDict | |
import itertools | |
import timeit | |
def keygen(): | |
"""A generator that produces all possible combinations of letters.""" |
This file contains 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
# Based on | |
# http://wiki.python.org/moin/PythonDecoratorLibrary#Memoize | |
class memoized(object): | |
"""Decorator that caches a function's return value each time it is called. | |
If called later with the same arguments, the cached value is returned, and | |
not re-evaluated. | |
""" | |
def __init__(self, func): |
This file contains 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
# Python 2.7 | |
from collections import OrderedDict | |
in_name = 'F87.p190' | |
out_name = 'my_results.txt' | |
results = OrderedDict() |
This file contains 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 csv | |
import requests | |
from BeautifulSoup import BeautifulSoup | |
from datetime import datetime | |
# http://www.reddit.com/r/learnpython/comments/tczgd/help_me_improve_this_code_webscraping_liquor/ | |
# http://pastebin.com/TinHnCSp | |
# http://www.specsonline.com/cgi-bin/snf?body=/cgi-bin/prodlist&index=Liquors%7C255%7CSCOTCH+MALTS |
This file contains 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
# Demonstration of inspecting all the routes, including those on sub-apps, | |
# from the default app instance. | |
# | |
# This should be run directly to print a list of route prefixes and the rules. | |
# Tested with Python 2.7 and Bottle-dev. Patch here | |
# https://github.com/davidwtbuxton/bottle/commit/ddd712ef252b06ecd0e957f8ac4e37b65ee79cae | |
import bottle | |
subapp = bottle.Bottle() |
This file contains 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 bottle | |
import functools | |
def decorate(func): | |
"""Prints the arguments.""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
print args, kwargs | |
return func(*args, **kwargs) |
OlderNewer