Skip to content

Instantly share code, notes, and snippets.

View erikbern's full-sized avatar

Erik Bernhardsson erikbern

View GitHub Profile
@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
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):
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 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()
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}')
@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}')
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 / loop_hack.py
Last active April 3, 2022 21:35
Example of how to use async/await programming in Python for non-asyncio purposes
# First, let's create an awaitable object.
# In this case it's a very dumb container of integers.
# Any time a coroutine runs `await Thing(n)` it just resolves to n
# However, we could resolve it to something completely different if we wanted to
class Thing:
def __init__(self, n):
self._n = n
def __await__(self):
@erikbern
erikbern / asyncio_coroutine_interceptor.py
Last active June 2, 2022 19:39
Send data to coroutines that do async things
async def intercept_coro(coro, interceptor):
# This roughly corresponds to https://gist.github.com/erikbern/ad7615d22b700e8dbbafd8e4d2f335e1
# The underlying idea is that we can execute a coroutine ourselves and use it to intercept
# any awaitable object. This lets the coroutine await arbitrary awaitable objects, not just
# asyncio futures. See how this is used in object.load.
value_to_send = None
while True:
try:
awaitable = coro.send(value_to_send)
assert inspect.isawaitable(awaitable)
@erikbern
erikbern / kaplan_meier_for_revenue.py
Last active October 14, 2023 19:04
Kaplan-Meier for multiple revenue events
from matplotlib import pyplot
import random
import time
pyplot.style.use("ggplot")
now = time.time()
def generate_user(censor=now):
# Pick some point in time the user was created
t_created = t = now - random.random() * 1e7