Skip to content

Instantly share code, notes, and snippets.

@danielhfrank
danielhfrank / trio_fib.py
Created December 1, 2020 20:53
Implementations of concurrent fibonacci numbers in vanilla asyncio and trio
import asyncio
import trio
import sys
async def fib(n: int) -> int:
if n < 0:
raise ValueError('boom')
elif n in {0, 1}:
@danielhfrank
danielhfrank / pydantic.py
Created December 1, 2020 00:15
Pydantic with Numpy
from typing import Generic, TypeVar
import numpy as np
from pydantic.fields import ModelField
JSON_ENCODERS = {
np.ndarray: lambda arr: arr.tolist()
}
DType = TypeVar('DType')
package com.stripe.danielhfrank
import com.twitter.algebird.Monoid
import scala.annotation.tailrec
import scala.collection.mutable
class OverTimeMonoid[V: Monoid] extends Monoid[ Seq[(Long, V)]] {
override def zero: Seq[(Long, V)] = Seq.empty
#!/usr/bin/env ruby
PENDING=2
NO_STATUS=3
return_txt = ""
loop do
return_txt = `hub ci-status -v`
break unless [PENDING, NO_STATUS].include?($?.exitstatus)
sleep(1)
#!/usr/bin/env sh
while [ `hub ci-status` = "pending" ]; do
sleep 5
done
terminal-notifier -message "Build finished"
@danielhfrank
danielhfrank / keybase.md
Created March 20, 2014 16:16
keybase.md

Keybase proof

I hereby claim:

  • I am danielhfrank on github.
  • I am df (https://keybase.io/df) on keybase.
  • I have a public key whose fingerprint is 6627 401D 1986 3A54 3235 A733 5B08 4339 A82D F24E

To claim this, I am signing this object:

@danielhfrank
danielhfrank / passthrough_errors.py
Created June 18, 2013 21:36
This decorator is intended to simplify error handling in request cycles with many callbacks. The idea is to decorate a callback function that takes its main "data" as a first positional argument, and a callback as a kwarg called "callback". The decorator will inspect the data argument and attempt to determine if it is an error (either a tornado…
def passthrough_errors(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
callback = kwargs['callback']
data = args[0]
# Check if an error is being passed in, somehow
error = None
if isinstance(data, tornado.httpclient.HTTPResponse) and data.error:
error = data.error
@danielhfrank
danielhfrank / doom.py
Created May 9, 2013 20:50
Attempt at a retry-with-backoff decorator. May it (and I) rest in peace
def retry_with_backoff(method):
"""
The idea was to use this to decorate a method that makes some kind of request and has a callback
kwarg. The callback will be decorated to retry the calling function a few times,
with backoff, if it receives an error.
"""
@functools.wraps(method)
def caller_wrapper(self, *args, **kwargs):
callback = kwargs['callback']
attempts = kwargs.pop('attempts', 0)
@danielhfrank
danielhfrank / unit_interval_scaler.py
Last active December 14, 2015 01:49
Scale to unit interval. Screw the mean and variance
class UnitIntervalScaler(object):
def fit(self, xs):
self._floor = np.min(xs)
self._range = np.max(xs) - self._floor
return self
def transform(self, x):
return (x - self._floor) / self._range
@danielhfrank
danielhfrank / cls_test.py
Created January 30, 2013 23:13
Methods not defined within class don't pass self as first arg
class X(object):
def __str__(self):
return 'x'
def my_print_arg(*args):
print args[0]
def print_arg(*args):
print args[0]