Skip to content

Instantly share code, notes, and snippets.

View erikbern's full-sized avatar

Erik Bernhardsson erikbern

View GitHub Profile
import asyncio
class AsyncConstructorMeta(type):
"""Metaclass to support asynchronous constructors in Python.
Basically we're exploiting the fact that __new__ can return anything in Python.
So we're taking the old __init__ code, removing it from the class, and instead,
we create a custom __new__ method that returns a coroutine wrapping the original
constructor.
@erikbern
erikbern / take_over_globals.py
Last active November 5, 2021 12:32
Just a proof of concept of how you can inject your own "storage engine" for global variables
class MyDict(dict):
def __init__(self):
self._dict = {}
def __getitem__(self, k):
print(f'Looking up {k}')
return self._dict[k]
def __setitem__(self, k, v):
print(f'Assigning {k} to {v}')
class MyDict(dict):
def __init__(self):
self._dict = {}
def __getitem__(self, k):
print(f'Looking up {k}')
return self._dict[k]
def __setitem__(self, k, v):
print(f'Assigning {k} to {v}')
import random
import numpy
from matplotlib import pyplot
rs = numpy.random.randn(1000)
xs = rs[1:-1] - rs[:-2]
ys = rs[2:] - rs[1:-1]
pyplot.scatter(xs, ys)
pyplot.show()
import numpy
import scipy.optimize
from matplotlib import pyplot
cs = numpy.linspace(0.01, 0.99, 100)
ks = []
for c in cs:
def f(log_k):
k = numpy.exp(log_k)
import numpy
import random
from matplotlib import pyplot
percentiles = [50, 75, 90, 95, 99]
latencies = [[] for p in percentiles]
loads = []
n = 100000
for k in numpy.linspace(0.01, 1.0, 100):
@erikbern
erikbern / american_community_survey_example.py
Last active May 1, 2022 18:17
Download and parse American Community Survey data using Python
# Uses American Community Survey data to estimate property taxes
# https://www.census.gov/programs-surveys/acs/
# The data is a f-ing PITA to parse, but here's an attempt
import bs4
import csv
import io
import os
import requests
import sys
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')
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)
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])