Skip to content

Instantly share code, notes, and snippets.

View ehoppmann's full-sized avatar

Emma Hoppmann ehoppmann

View GitHub Profile
@ehoppmann
ehoppmann / convertWeightRecorderXMLtoCSV.py
Last active September 19, 2016 15:16
Convert iOS Weight Recorder / WeightRecord XML file into CSV
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import dateparser
import csv
soup = BeautifulSoup(open('WeightRecorder - 2016-09-18.xml'), "lxml")
def kgtolbs(kg):
return float(kg) * 2.20462262185
@ehoppmann
ehoppmann / pound_https_domains.sh
Created March 19, 2017 18:07
Extract all ssl https hosts from pound proxy config file in preparation for running letsencrypt
#!/bin/bash
grep -A10000000 ListenHTTPS /etc/pound/pound.cfg \
| sed -n "s/^.*HeadRequire\s*\"Host\:\.\*\(.*\)\.\*.*$/\1/p" \
> domains.txt
@ehoppmann
ehoppmann / slack_history.py
Created August 22, 2017 18:19 — forked from Chandler/slack_history.py
Download Slack Channel/PrivateChannel/DirectMessage History
# MIT License
# Copyright (c) 2016 Chandler Abraham
# 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, including 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:
@ehoppmann
ehoppmann / pandas heatmaps
Created August 22, 2017 18:55 — forked from s-boardman/pandas heatmaps
Heatmap functions for Pandas dataframes.
"""Plots a Pandas dataframe as a heatmap"""
import matplotlib as mpl
import matplotlib.pyplot as plt
def heatmap(df,
edgecolors='w',
cmap=mpl.cm.RdBu,
log=False,vmin=0,vmax=500):
width = len(df.columns)/4
height = len(df.index)/4
@ehoppmann
ehoppmann / ordered_set.py
Last active August 13, 2018 14:51
Returns a list containing only unique elements from original list, with an optional key function
from typing import Iterable, Callable, List
def ordered_set(iterable: Iterable, key: Callable=lambda x: x) -> List:
"""
Returns a new list containing all unique items from the iterable. A custom key function can
be specified to customize the uniqueness constraint, for example to return the first unique
element from the input list based on the second item in each element, one could pass
`lambda x: x[1]`.
"""
out_list = []
@ehoppmann
ehoppmann / awslogs_to_lambda_cost.py
Created August 29, 2018 16:35
Compute lambda cost from logs downloaded using awslogs
COST_GB_SEC = 0.00001667
cost = 0
with open('logs.log', 'r') as f:
logs = f.readlines()
billing = [i.split('\t') for i in logs if '\tBilled Duration:' in i]
for i in billing:
gb = float(i[3].lstrip('Memory Size: ').rstrip(' MB')) / 1024
sec = float(i[2].lstrip('Billed Duration: ').rstrip(' ms ')) / 1000
@ehoppmann
ehoppmann / scalebars.py
Created May 28, 2019 18:37 — forked from dmeliza/scalebars.py
matplotlib: add scale bars to axes
# -*- coding: utf-8 -*-
# -*- mode: python -*-
# Adapted from mpl_toolkits.axes_grid1
# LICENSE: Python Software Foundation (http://docs.python.org/license.html)
from matplotlib.offsetbox import AnchoredOffsetbox
class AnchoredScaleBar(AnchoredOffsetbox):
def __init__(self, transform, sizex=0, sizey=0, labelx=None, labely=None, loc=4,
pad=0.1, borderpad=0.1, sep=2, prop=None, barcolor="black", barwidth=None,
**kwargs):
@ehoppmann
ehoppmann / hpd.py
Created June 4, 2019 18:28
Calculate highest posterior density (HPD) of array for given alpha. From Bayesian Analysis with Python: https://github.com/aloctavodia/BAP/blob/master/first_edition/code/Chp1/hpd.py
from __future__ import division
import numpy as np
import scipy.stats.kde as kde
def hpd_grid(sample, alpha=0.05, roundto=2):
"""Calculate highest posterior density (HPD) of array for given alpha.
The HPD is the minimum width Bayesian credible interval (BCI).
The function works for multimodal distributions, returning more than one mode
Parameters
@ehoppmann
ehoppmann / slacktheme.md
Created June 18, 2019 19:24 — forked from thibmaek/slacktheme.md
Slack Color Schemes

Kimbie

#F3E3CD,#F3E3CD,#F3951D,#DA3D61,#F26328,#183E1C,#DA3D61,#F26328 screen

Monokai

#222222,#2F2F2F,#F92772,#FFFFFF,#A6E22D,#FFFFFF,#66D9EF,#BE84F2 screen

Juice Bar

#86A34E,#94AF63,#FFFFFF,#6D8B42,#94AF63,#FFFFFF,#FFB10A,#DFA044

@ehoppmann
ehoppmann / self_restarting_python_script.py
Created June 25, 2020 15:58
A python script that restarts itself (running a new process that reflects any changes on disk)
#!/usr/bin/env python3
# based on https://github.com/cherrypy/cherrypy/blob/0857fa81eb0ab647c7b59a019338bab057f7748b/cherrypy/process/wspbus.py#L305
import sys
import os
import time
_startup_cwd = os.getcwd()
def _do_execv():
args = sys.argv[:]