Skip to content

Instantly share code, notes, and snippets.

View erikbern's full-sized avatar

Erik Bernhardsson erikbern

View GitHub Profile
@erikbern
erikbern / kaplan_meier.py
Created May 22, 2017 01:59
Kaplan-Meier snippet
n, k = len(te), 0
ts, ys = [], []
p = 1.0
for t, e in te:
if e: # whether the event was "observed" (converted) or not observed (may convert in the future)
p *= (n-1) / n
n -= 1
ts.append(t)
ys.append(100. * (1-p))
pyplot.plot(ts, ys, 'b')
@erikbern
erikbern / get_n_results.py
Created March 13, 2017 02:34
Get number of search results from Google
def get_n_results_dumb(q):
r = requests.get('http://www.google.com/search',
params={'q': q,
"tbs": "li:1"})
r.raise_for_status()
soup = bs4.BeautifulSoup(r.text)
s = soup.find('div', {'id': 'resultStats'}).text
if not s:
return 0
m = re.search(r'([0-9,]+)', s)
@erikbern
erikbern / group-rotation.js
Last active October 20, 2016 19:03
Update a Google group to contain only one single member
/*
This code is meant to be used with a rotating responsibility for incoming bugs and stuff.
To make this work, you need to:
1. Set up the key in the Google IAM manager
2. Add domain delegation for the user
3. Download the key and add the following fields: "admin_email" and "domain"
*/
var key = require('./google-people-api-creds.json');
var scope = ['https://www.googleapis.com/auth/admin.directory.group',
import numpy
from matplotlib import pyplot
import seaborn
p = numpy.logspace(-6, 0, 50)
pyplot.figure(figsize=(10, 5))
for k in [1, 3, 10, 30, 100, 300, 1000]:
Z = (p * k**2 + (1 - p)) / (p * k + (1 - p))
import numpy
from matplotlib import pyplot
import seaborn
delta = 0.1
x = numpy.arange(0.0, 10.0, delta)
y = numpy.arange(0.0, 10.0, delta)
X, Y = numpy.meshgrid(x, y)
p = 0.5
@erikbern
erikbern / use_pfx_with_requests.py
Last active June 5, 2024 12:18
How to use a .pfx file with Python requests – also works with .p12 files
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
@contextlib.contextmanager
def pfx_to_pem(pfx_path, pfx_password):
''' Decrypts the .pfx file to be used with requests. '''
import urllib2, csv
import matplotlib.pyplot as plt
import datetime
import seaborn
import numpy, scipy.stats, math
f = urllib2.urlopen('https://raw.githubusercontent.com/datasets/s-and-p-500/master/data/data.csv')
csv = csv.reader(f)
csv.next() # headers
from google.transit import gtfs_realtime_pb2
import urllib
for feed_id in [1, 2, 11]:
feed = gtfs_realtime_pb2.FeedMessage()
response = urllib.urlopen('http://datamine.mta.info/mta_esi.php?key=%s&feed_id=%d' % (os.environ['MTA_KEY'], feed_id))
feed.ParseFromString(response.read())
print feed
import os, sys, re
patterns = [
# <img src='http://s.wordpress.com/latex.php?latex=%5Cmathcal%7BO%7D%28n%29&#038;bg=T&#038;fg=000000&#038;s=0' alt='\mathcal{O}(n)' title='\mathcal{O}(n)' class='latex' />
(r'<img.*?title=\'(.*?)\' class=\'latex\' />',
'$$ \\1 $$ ',
0),
# [<img class=" size-full wp-image-1722 aligncenter" src="http://erikbern.com/wp-content/uploads/2016/01/avg.png" alt="avg" width="512" height="512" />](http://erikbern.com/wp-content/uploads/2016/01/avg.png)
(r'\[(<img.*?)\]\(.*?\)',
import pymc3, numpy, sys, seaborn, re
def get_dist(fn):
y = [0, 0, 0, 0, 0]
for line in open(fn):
try:
num = re.split('\D', line)[0]
y[int(num) - 1] += 1
except:
print fn, 'can not parse:', line