Skip to content

Instantly share code, notes, and snippets.

View hughdbrown's full-sized avatar

Hugh Brown hughdbrown

View GitHub Profile
@hughdbrown
hughdbrown / apply_grep_script.py
Last active May 11, 2018 15:50
Apply a set of changes in grep -n format to a file
"""
Given a set of file names, line numbers, and lines of text in `grep -n` format,
update the file with altered text.
Suppose you have added a bunch of lines of code to your code base that have double
quotes when it is your company practice to use single quotes. When you are cleaning
up your code for a pull-request, you find all the changes with double quotes,
drop extraneous lines, change all the double quotes to single quotes in the
grep output, and use this python code to apply changes to your files.
@hughdbrown
hughdbrown / lru_cache.py
Last active February 27, 2024 17:36
Minimal lru_cache implementation for python 2.7
from functools import wraps
try:
from functools import lru_cache
except ImportError:
def lru_cache(user_function):
cache = {}
@wraps(user_function)
def wrapper(*args):
key = tuple(args)
@hughdbrown
hughdbrown / list_of_dict_grouped_by_series_id.py
Last active April 27, 2018 00:34
Create a list of dictionaries keyed by series_id
import pandas as pd
df = pd.DataFrame({
'date': list(range(20)),
'value': list(reversed(range(20))),
'series_id': [1, 2, 3, 4] * 5,
})
gr = df.groupby(["series_id"])
hist = {
@hughdbrown
hughdbrown / patch_start.py
Created March 22, 2018 04:44
Locate tests in which patch.start is called but patch.stop may not be
#!/usr/bin/env python
import logging
import os
import os.path
import re
from itertools import tee, izip
re_class_start = re.compile(r'^class\s+(?P<name>[^(]+).*$')
re_func_start = re.compile(r'^\s*def\s+(?P<name>[^(]+).*$')
re_patch_start = re.compile(r'^.*patch.*\.start\(.*$')
@hughdbrown
hughdbrown / install-ripgrep.sh
Created March 9, 2018 18:48
Install ripgrep on ubuntu
#!/bin/bash
[[ $UID == 0 ]] || { echo "run as sudo to install"; exit 1; }
REPO="https://github.com/BurntSushi/ripgrep/releases/download/"
RG_LATEST=$(curl -sSL "https://api.github.com/repos/BurntSushi/ripgrep/releases/latest" | jq --raw-output .tag_name)
RELEASE="${RG_LATEST}/ripgrep-${RG_LATEST}-x86_64-unknown-linux-musl.tar.gz"
TMPDIR=$(mktemp -d)
cd $TMPDIR
wget -O - ${REPO}${RELEASE} | tar zxf - --strip-component=1
@hughdbrown
hughdbrown / prediction-api-curl.sh
Created March 7, 2018 16:16
Predictions API usage from command line
#!/bin/sh
# Cat a JSON file and send it as the data argument to curl in a
# POST request to DataRobot prediction API
cat <JSON-FILE> |
curl -H "Content-Type: application/json" \
-X POST -d @- \
-u <USER-NAME>:<API-KEY> \
http://<HOST>/predApi/v1.0/<PROJECT-ID>/<MODEL-ID>/predict
@hughdbrown
hughdbrown / checkpoint_mongo.py
Last active March 2, 2018 14:57
Code to checkpoint and diff mongodb databases
#!/usr/bin/env python
"""
Python module to create and diff checkpoints on mongo databases.
"""
import logging
# import json
from uuid import uuid4
from pprint import pprint
from datetime import datetime # pylint: disable=unused-import
@hughdbrown
hughdbrown / jupyter-lab.sh
Created November 14, 2017 19:47
Set up virtualenv and jupyter kernel to run isolated jupyter lab
#!/bin/sh
# Based on:
# http://anbasile.github.io/programming/2017/06/25/jupyter-venv/
mkvirtualenv --python=`which python3` ADRPython
workon ADRPython
pip3 install \
jupyter jupyterlab jupyterthemes \
scipy \
@hughdbrown
hughdbrown / factors-any-base.py
Created November 2, 2017 04:59
Calculate the values for a base encoding
def numberToBase(n, b):
def _divmod(n, b):
while n:
n, q = divmod(n, b)
yield q
return [0] if not n else list(reversed(list(_divmod(n, b))))
@hughdbrown
hughdbrown / git-bisect-pred-1765.py
Last active October 18, 2017 20:21
git-bisect function to find error that occurs in web browser
#!/usr/bin/env python
"""
git-bisect code for PRED-1765
"""
import os
import os.path
import sys
import subprocess
import logging
from datetime import datetime