This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 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') |
This file contains hidden or 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 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) |
This file contains hidden or 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
| 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]) |