Skip to content

Instantly share code, notes, and snippets.

@selwyth
selwyth / caching.py
Created February 6, 2018 22:15
caching
# Utility function to help with local prototyping to minimize trips to fetch data.
# Import the function and decorate any data-retrieval function to use cache if
# available, make one if not. Remove the decorator before submitting to production.
import cPickle as pickle
import logging
import os
from functools import wraps
THIS_DIR = os.getcwd()
@selwyth
selwyth / with_query_rewriter.py
Created January 18, 2018 00:24
Rewrite your PostgreSQL query utilizing WITH syntax into a more efficient one that utilizes temporary tables.
import argparse
import os
import re
def main(sql_file):
with open(sql_file, 'r') as f:
sql = f.read()
sql = sql.upper().replace('WITH', ',')
@selwyth
selwyth / subplots.py
Last active August 29, 2015 14:21
Subplots
# source: http://matplotlib.org/examples/pylab_examples/demo_tight_layout.html
# define initially
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
# MATLAB numbering
fig = plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
@selwyth
selwyth / expand_grid.py
Last active August 29, 2015 14:21
Generating different types of data to illustrate examples or create a grid to conform to
# http://pandas.pydata.org/pandas-docs/stable/cookbook.html/ bottom example
def expand_grid(data_dict):
rows = itertools.product(*data_dict.values())
return pd.DataFrame.from_records(rows, columns=data_dict.keys())
df = expand_grid(
{'height': [60, 70],
'weight': [100, 140, 180],
'sex': ['Male', 'Female']})
@selwyth
selwyth / bggxml_collquery.py
Last active February 19, 2024 19:55
Using BGG XML API2
user = raw_input('Enter BGG username')
url = 'http://www.boardgamegeek.com/xmlapi2/collection?username='
import requests
import time
# return collection for any user, but wait 2 seconds and retry if error. 10 total attempts.
def coll_final(USER):
for i in range(10): # try 10 times
r = requests.get(url + USER + "&rated=1&excludesubtype=boardgameexpansion&stats=1")