Skip to content

Instantly share code, notes, and snippets.

View erikbern's full-sized avatar

Erik Bernhardsson erikbern

View GitHub Profile
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
@erikbern
erikbern / use_pfx_with_requests.py
Last active April 24, 2024 13:48
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 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
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))
@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',
@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 / 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')
def ll_to_3d(lat, lon):
lat *= math.pi / 180
lon *= math.pi / 180
x = math.cos(lat) * math.cos(lon)
z = math.cos(lat) * math.sin(lon)
y = math.sin(lat)
return numpy.array([x, y, z])
import matplotlib.pyplot, numpy, scipy.stats, seaborn
for i in range(50):
p, = scipy.stats.uniform.rvs(size=1)
a, b = scipy.stats.geom.rvs(p, size=2)
if a == b == 1:
continue
print(a, b)
x = numpy.linspace(0, 1, 1000)
y = scipy.stats.beta.pdf(x, a, b)
from num2words import num2words
from matplotlib import pyplot
lang = 'de'
words = [num2words(i, lang=lang) for i in range(1000000)]
fig = pyplot.figure()
ax = fig.add_subplot(111)
ax.semilogx([len(word) for word in words], color='green')