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 multiprocessing | |
class Consumer(multiprocessing.Process): | |
"""Consumer processes. Subclassed from Process in multiprocessing.""" | |
def __init__(self, task_queue, result_queue): | |
multiprocessing.Process.__init__(self) | |
self.task_queue = task_queue | |
self.result_queue = result_queue |
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 numpy as n | |
import pylab as p | |
from matplotlib.patches import Rectangle | |
from matplotlib.collections import PatchCollection | |
from pitch import create_normalized_pitch | |
def _add_centered_square(xy, area): | |
size = n.sqrt(area) |
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 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 |
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
# 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. |
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
{ | |
"version": 1, | |
"disable_existing_loggers": false, | |
"formatters": { | |
"default": { | |
"format": "%(asctime)s - %(name)s - %(levelname)s: %(message)s" | |
}, | |
"console": { | |
"format": "%(name)-12s: %(message)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
from lxml import etree | |
from lxml.builder import E | |
if __name__ == "__main__": | |
with open('sample.xml') as f: | |
xml = f.read() | |
new_xml = E.Wrapper(etree.XML(xml)) | |
print(etree.tostring(new_xml, pretty_print=True)) | |
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 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 |
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
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']] | |
else: |
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 Python context to move in and out of directories. | |
Copyright 2016-2020, Howard Hamilton. | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | |
associated documentation files (the "Software"), to deal in the Software without restriction, in- | |
cluding without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: |
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
""" | |
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 (http://choosealicense.com/licenses/mit/) | |
""" | |
from sklearn.neighbors import NearestNeighbors |
NewerOlder