Instantly share code, notes, and snippets.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pch
import matplotlib.image as img
def create_normalized_pitch(size=(12.5, 8.0), pcolor='none', ecolor='black'):
Create figure that has football pitch lines drawn on it. Figure coordinates are normalized to a 100x100 grid.
If aspect ratio of figure is not equal to 1.5625 (12.5 in / 8.0 in), the figure width is
# unique list of dictionaries
# ---------------------------
# This problem comes up when I bulk insert records into databases
# because I can't check for existence of records (they don't exist yet, too slow, etc)
# I typically use a set to make list elements unique, but dicts and lists themselves
# can't be inserted in sets because they're mutable and thus unhashable.
# Solution is to make a tuple of the dict's items and insert it in set,
# then create dicts from each member of the set.
View logging.json
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"default": {
"format": "%(asctime)s - %(name)s - %(levelname)s: %(message)s"
"console": {
"format": "%(name)-12s: %(message)s"
from lxml import etree
from lxml.builder import E
if __name__ == "__main__":
with open('sample.xml') as f:
xml =
new_xml = E.Wrapper(etree.XML(xml))
print(etree.tostring(new_xml, pretty_print=True))
import logging
import subprocess
from io import BytesIO
def execute_command(cmd, ns=__name__, shell_flag=False):
Execute command using a subprocess, and write streaming output to the screen.
:param cmd: Command-line statement
class RegexpReplacer(object):
Take in list or dictionary of regex patterns and substitutes, and apply them to text with a map.
def __init__(self, patterns):
if type(patterns) is dict:
self.patterns = [(re.compile(r"{}".format(regex)), patterns['replace'])
for regex in patterns['pattern']]
"""A Python context to move in and out of directories"""
import os
from contextlib import contextmanager
def pushd(new_dir):
previous_dir = os.getcwd()
An outline for identifying N players with similar summary statistics to a player of interest.
A list of candidate players is compiled by filtering on position and end-of-season age.
Summary statistics are scaled to z-scores, which are the inputs to the machine learning model.
This algorithm uses K-Nearest Neighbor, but other algorithms (eg K-Means Clustering) can be substituted.
(c) 2015 Soccermetrics Research LLC
This code is licensed under the terms of the MIT License (
from sklearn.neighbors import NearestNeighbors
# I wrote this script in order to transfer commits between branches in a less painful way. The use
# case is rebasing a working branch on its parent in order to use changes that have been pushed there.
# Yes, a rebase does preserve history, but only if you haven't already pushed the branch to a remote.
# If you have, it's best to create a fresh branch from the parent and transfer the commits there.
# My tasks are tracked in JIRA, so I write all of my commits with the following convention:
# create trie data structure
class Node(object):
Node element of trie data structure.