Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
curl \
--write-out "%{http_code},%{time_namelookup},%{time_appconnect},%{time_connect},%{time_appconnect},%{time_starttransfer},%{time_total}\n" \
--silent \
--output /dev/null \
-L
"$1"
@wheaties
wheaties / rewrite_history.py
Last active December 6, 2018 15:22
Phabricator hints.json rewrite script
#Assumes that the branches already exist
#Assumes that files contain the contents of git log --format=oneline
import argparse
import json
parser = argparse.ArgumentParser(description='Phabricator orphan branch file generator')
parser.add_argument('original', help='The unmodified original branch git log')
parser.add_argument('orphan', help='The rebased --onto branch git log')
parser.add_argument('repository', help='The repository')
@wheaties
wheaties / field.py
Created October 25, 2018 01:52
Partially Persistent Data
class Field:
def __get__(self, obj, obj_type=None):
if not obj:
return self
return getattr(obj.view(), self.__name__)
def __set__(self, obj, value):
obj.update(self.__name__=value)
@wheaties
wheaties / write_once.py
Last active September 10, 2018 20:47
Python write once field
from weakref import WeakKeyDictionary
from collections.abc import Hashable
#Only works with hashable objects
class WriteOnce:
def __init__(self):
self.__values = WeakKeyDictionary()
def __get__(self, obj, obj_type=None):
if obj_type is not None:
@wheaties
wheaties / param.py
Created April 24, 2018 21:11
Python descriptor object for representing a url parameter
class Param:
'''Python descriptor for representing a query param within a url'''
def __init__(self, default=None, name=None, required=False):
self.default = str(default)
self.name = name
self.required = required
self.values = {} # in a real impl you'd use a WeakRefDict
def __set_name__(self, owner, name):
if not self.name:
@wheaties
wheaties / mkDefer.scala
Last active March 20, 2018 12:16
Playing in the compiler with by name parameters
//Trying to transform
// def foo(value: Int) = something(value) }
//into
// def foo(value: Int) = { def defer$[A](f: => A) = { () => f }; something(defer$(value)) }
def mkDefer(methSym: Symbol): Tree ={
val sym = methSym.newMethodSymbol(TermName("defer$"), newFlags = ARTIFACT)
val tpe = sym.newAbstractType(TypeName("A"), newFlags = PARAM)
tpe.setInfo(TypeBounds.empty)
val tref = typeRef(NoPrefix, tpe, Nil)
@wheaties
wheaties / blah.vcl
Last active February 15, 2018 21:43
Cache both HEAD and GET requests separately in Fastly
sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.host;
if (req.http.request-method) {
set req.hash += req.http.request-method;
}
return (lookup);
}
sub vcl_miss {
@wheaties
wheaties / catsPartial.scala
Last active November 9, 2017 21:23
Laziness and partial application
import cats.data.Kleisli
import cats.Eval
type Partial[A, B] = Kleisli[Eval, A, B]
object Partial{
def apply[A, S, B](f: (S, A) => B)(s: Eval[S]): Partial[A,B] = left(f)(s)
def right[A, S, B](f: (A, S) => B)(s: Eval[S]): Partial[A, B] = Kleisli(a => s.map(f(a, _)))
def left[A, S, B](f: (S, A) => B)(s: Eval[S]): Partial[A, B] = Kleisli(a => s.map(f(_, a)))
}
@wheaties
wheaties / StackOverflow.scala
Created April 20, 2017 02:27
Wherein I place something that causes a StackOverflow in Uncurry
def mapParams(methSym: Symbol, tree: DefDef) ={
val DefDef(_, _, _, vp :: vps, _, _) = tree
val refTree = gen.mkAttributedRef(tree.symbol.owner.thisType, methSym)
val forwarderTree = (Apply(refTree, vp map forwardArg) /: vps){
(fn, params) => Apply(fn, params map forwardArg)
}
deriveDefDef(tree)(_ => localTyper.typedPos(tree.symbol.pos)(forwarderTree))
}
def forwardArg(param: Tree): Tree =
@wheaties
wheaties / ContK.scala
Last active April 17, 2017 19:08
FunctionK Continuation
import cats.~>
//what would happen if you tried to make a continuation based on natural transforms...
abstract class ContK[F[_], G[_], R]{ self =>
def apply(fg: F ~> G): G[R]
def map[B](f: R => B)(implicit fn: Functor[G]) = new ContK[F, G, B]{
def apply(fg: F ~> G) = fn.map(self(fg))(f)
}