Skip to content

Instantly share code, notes, and snippets.

@danielrichman
danielrichman / gcd.ml
Created October 18, 2014 19:47
Euclid's algorithm
open Core.Std
(* r = t * a + s * b *)
type step = { r : int; t : int; s : int }
let euler a b =
let rec loop cur prev history =
(* prev.r = q * cur.r + next.r *)
let q = prev.r / cur.r in
(* next.r = prev.r - q * cur.r
@danielrichman
danielrichman / flask_no_cache.py
Last active January 22, 2018 06:14
flask no-cache headers
@app.after_request
def add_no_cache(response):
if request.endpoint != "static":
response.headers[b"Cache-Control"] = "no-cache"
response.headers[b"Pragma"] = "no-cache"
return response
@danielrichman
danielrichman / Makefile
Last active January 1, 2016 17:29
IPYNB -> Latex nbconvert template for CATAM
#!/usr/bin/make
# -*- makefile -*-
figures = $(wildcard figure_*.pdf)
report.ipynb.tex : report.ipynb ../nbconvert.tplx
ipython3 nbconvert \
--to latex \
--template nbconvert \
--ExtractOutputTransformer.enabled=False \
@danielrichman
danielrichman / history.coffee
Created December 12, 2013 16:53
CoffeeScript to HTML5-history-ify a series of static pages. All the content is included in each page, but /section1, say, will have #section1 visible. The code hooks links and instead just hides/shows the relevant section, with optional animations.
current_section = null
static_section = null
sections = null
url_map = {}
history_enable = null
hash_enable = null
log = (msg) -> console?.log? "history.coffee: #{msg}"
change = (to, animation="change") ->
@danielrichman
danielrichman / rebase.py
Last active December 25, 2015 17:39
re-base a class that uses super() in order to unit test it
from __future__ import print_function
# some library
class Base(object):
def method(self):
return "This is hard to stub"
# my code
class UUT(Base):
def method(self):
@danielrichman
danielrichman / static_raven_server.py
Created September 12, 2013 11:11
python-raven (+flask) replacement for .htaccess+mod_ucam_webauth
import os
from os.path import dirname, realpath
from os.path import join as path_join
from flask import Flask
from flask.helpers import send_from_directory
from raven.flask_glue import AuthDecorator
app = Flask(__name__)
assert not app.has_static_folder
@danielrichman
danielrichman / nose_dictdiff.py
Last active December 21, 2015 12:49
For when an assertion comparing two massive dicts fails in a python unit test
import sys
import datetime
# expect either a single line >> assert {.... dicta ....} == {.... dictb ....}
# (as produced from nosetests -d assertion failures)
# or two dicts, on two separate lines.
# obviously the former breaks if == appears in a key or value...
line = sys.stdin.readline()
@danielrichman
danielrichman / python_format_datetime.py
Created August 7, 2013 17:25
pretty python datetime formatting
def format_datetime(datetime, format_name="nice short",
custom_format=None, convert_bst=True):
"""
Jinja2 filter to turn a datetime into a string
`datetime` must be timezone-naive (as far as python is concerned),
but its values must be UTC.
Either pick `format_name` from the options below, or provide
`custom_format`
@danielrichman
danielrichman / templated_email.py
Created August 7, 2013 12:11
Sending emails, bodies generated by jinja2, with a utility to wrap text nicely.
def send_email(template, recipient,
sender=("Jinja email Gist", "[email protected]"),
**kwargs):
"""
Send an email, acquiring its payload by rendering a jinja2 template
:type template: :class:`str`
:param template: name of the template file in ``templates/emails`` to use
:type recipient: :class:`tuple` (:class:`str`, :class:`str`)
:param recipient: 'To' (name, email)
@danielrichman
danielrichman / postgres_contexthelpers.py
Last active December 20, 2015 15:39
2 contextmanager helpers to run PostgreSQL queries with savepoints and deferred constraints
@contextlib.contextmanager
def with_savepoint(postgres, name="temp_savepoint"):
"""
A context manager that wraps database queries in a PostgreSQL savepoint
Usage::
with with_savepoint(postgres, name='my_savepoint) as rollback:
do_something()
if something: