Skip to content

Instantly share code, notes, and snippets.

@danielrichman
danielrichman / habitat-database-rebuild-notes.md
Last active July 5, 2020 00:18
habitat database rebuild notes

Instructions

import time
import shlex
class CPIOWriter:
# https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
HEADER_FORMAT = "070701{ino:08X}{mode:08X}{uid:08X}{gid:08X}{nlink:08X}{mtime:08X}" \
"{filesize:08X}{maj:08X}{min:08X}{rmaj:08X}{rmin:08X}{namesize:08X}{chksum:08X}"
OTHER_HEADER_ARGS = \
{ "mode": 0o100000 + 0o644 # regular file + perms, see stat(2)
, "uid": 0
@danielrichman
danielrichman / babyasync.ml
Last active July 7, 2018 12:02
baby async+epoll implementation, and proof-of-concept web server using it
open Core
module Monitor = struct
type t = unit
end
module Scheduler0 : sig
val enqueue
: (unit -> unit)
-> Monitor.t
@danielrichman
danielrichman / example.yaml
Last active February 22, 2020 19:43
templated mail merging with postgres & jinja2
query: SELECT * FROM people WHERE condition
sender:
name: Daniel Richman
email: main@danielrichman.co.uk
to_email_keys: [homeemail, workemail]
to_name_template: |
{{ firstname }} {{ surname }}
subject_template: Hello {{ firstname }}
body_template: |
Hello, {% include "to_name" %}
@danielrichman
danielrichman / foosball_elo.py
Created July 5, 2015 13:40
Standalone (w/o Postgres) tool to play with foosball scores. See https://github.com/joey9801/stat-tracker.
from __future__ import division, print_function
import json
import numpy as np
from scipy.stats import norm
class config:
def no_points_condition(reds, blues):
return "X" in reds or "X" in blues
@danielrichman
danielrichman / varnish_timing.py
Created March 20, 2015 13:32
varnish timing stats
# varnishncsa -a -w /var/log/varnish/timing.log -F "%{Varnish:handling}x %{Varnish:time_firstbyte}x %r"
from __future__ import print_function
def timing_log():
with open("/var/log/varnish/timing.log") as f:
for line in f:
hitmiss, time, url = line.split(" ", 2)
if hitmiss != "miss":
continue
@danielrichman
danielrichman / postgresqlhandler.py
Last active May 8, 2021 18:19
python postgres log handler
import logging
import traceback
import psycopg2
class PostgreSQLHandler(logging.Handler):
"""
A :class:`logging.Handler` that logs to the `log` PostgreSQL table
Does not use :class:`PostgreSQL`, keeping its own connection, in autocommit
mode.
@danielrichman
danielrichman / flaskadapter.py
Last active December 16, 2020 19:55
logger adapters
class FlaskLoggerAdapter(logging.LoggerAdapter):
"""
A :class:`logging.LoggerAdapter` that adds request context
Adds the following attributes to log records (which can then be used in a
:class:`logging.Formatter` string).
* base_url: ``flask.request.base_url``
* flask_endpoint: ``flask.request.endpoint``
* remote_addr: ``flask.request.remote_addr``
@danielrichman
danielrichman / README.md
Last active December 23, 2018 02:43
Simple OCaml setup

Simple OCaml Setup

Goal: quickly install OCaml, Core, Async, and other JS libraries.

Plan: do it once, and then make a .deb that can be applied to other boxes.

It would be nicer if I could do the Debian packaging properly, but for now this is a quick fix.

Problems / warnings / notes

@danielrichman
danielrichman / app.py
Last active May 19, 2017 16:30
Flask-Micro-SQLAlchemy
# Extracted from Flask-SQLAlchemy
sess = sqlalchemy.orm.scoped_session(
database.Session,
scopefunc=flask._app_ctx_stack.__ident_func__
)
@app.teardown_appcontext
def teardown_appcontext(res):
session.remove()
return res