This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |