Skip to content

Instantly share code, notes, and snippets.

View howardhamilton's full-sized avatar
💭
Tap tap tap...is this mic on?

Howard Hamilton howardhamilton

💭
Tap tap tap...is this mic on?
  • Atlanta, GA, USA
View GitHub Profile
@howardhamilton
howardhamilton / num_line.py
Created March 18, 2014 03:56
Update list of number line intervals with addition of new interval
#!/usr/bin/python
#
# run 'python num_line.py -v' to see doctest results.
# Howard Hamilton
# 17 March 2014
def number_line(L, I):
'''
Update number line intervals with addition of new interval.
@howardhamilton
howardhamilton / trie.py
Created March 24, 2014 15:01
Create a trie data structure for storing a word dictionary
#
# trie.py
#
# create trie data structure
#
class Node(object):
"""
Node element of trie data structure.
@howardhamilton
howardhamilton / commit_transfer.py
Created January 27, 2015 18:05
Transfer git commits from one branch to another (useful for rebasing)
#
# commit_transfer.py
#
# 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:
#
@howardhamilton
howardhamilton / player_similarity.py
Last active November 9, 2017 17:42
Identify N nearest neighbors to player's season statistics
"""
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
@howardhamilton
howardhamilton / pushd.py
Last active May 2, 2023 02:56
A pushd/popd context for Python scripts
"""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:
@howardhamilton
howardhamilton / replacer.py
Created May 23, 2016 12:38
Apply regex patterns and substitutions to text with a map
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:
@howardhamilton
howardhamilton / exec_cmd.py
Last active November 3, 2016 21:14
Execute command using a subprocess and write streaming output to the screen
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
@howardhamilton
howardhamilton / xml_wrap.py
Created October 19, 2016 16:28
Pattern to wrap XML content in outer tag, in order to get attributes in original root tag
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))
@howardhamilton
howardhamilton / logging.json
Created October 19, 2016 16:33
Setup logging configuration
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"default": {
"format": "%(asctime)s - %(name)s - %(levelname)s: %(message)s"
},
"console": {
"format": "%(name)-12s: %(message)s"
}
@howardhamilton
howardhamilton / unique_dicts.py
Last active November 3, 2016 15:50
Unique list of dictionaries
# 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.