Skip to content

Instantly share code, notes, and snippets.

@phblj
phblj / coinflips.py
Last active January 2, 2016 20:59
Early test termination This is a simple script to illustrate the danger of early termination of a trial (a form of selection bias). The script takes a fair coin, and flips it *flips* times. Throughout the test, it watches to see if the current counts are ever "statistically significant (p <0.05)" to be more likely heads than tails. This is to em…
import math
import random
pdf_memo = {}
def pdf(h, n):
h = min(h, n-h)
if (h, n) not in pdf_memo:
pdf_memo[(h, n)] = math.factorial(n)/(math.factorial(h)*math.factorial(n-h))*(0.5**h)*(0.5**(n-h))
@phblj
phblj / schedulers.py
Last active July 11, 2023 13:00
SingleRunScheduler to remove SPOF of lone celerybeat server. Sorry it's a bit disjoint-- this is pulled out from our codebase, which has a pretty big chunk of custom orm in it
def get_last_run(task_name):
task = db.scheduled_tasks.find_one({'name': task_name})
if task is None:
last_run = time.time()
# race condition possible, but this only happens at first run
db.scheduled_tasks.update(
{
'name': task_name
},