Skip to content

Instantly share code, notes, and snippets.

@evansd
evansd / on_parent_exit.py
Created April 9, 2012 21:19
Ensure subprocesses exit when their parents die
"""
Utility (Linux only) to ensure subprocesses exit when their parents do by sending
a specified signal when the parent dies.
Usage:
subprocess.Popen(['some-executable'], preexec_fn=on_parent_exit('SIGHUP'))
"""
import signal
import pickle
import platform
import struct
import timeit
import warnings
import numpy
import pyarrow
from scipy.sparse import csc_matrix
#!/usr/bin/env python
import fileinput
import datetime
THRESHOLD = 50000
MARKER = "==> "
SPACER = " " * len(MARKER)
last_ts = None
@evansd
evansd / development-services.sh
Last active January 14, 2022 10:08
Expects to be run from a `bin/` subdirectory within the main project directory
#!/bin/bash
set -euo pipefail
help_text="
Usage: bin/development-services [-h|--help] [-n|--no-attach]
Executes the commands listed in the 'Procfile.development' file, each in its
own pane of a new tmux session, and then attaches to that session.
If an appropriate tmux session already exists then we attach to it without
"""
Simple proof-of-concept for zero-copy unpickling
"""
import pickle
import struct
def serialize(obj):
buffers = []
pickled = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL, buffer_callback=buffers.append)
@evansd
evansd / bmi.sql
Last active February 15, 2021 13:06
somesql.sql
SELECT * INTO OPENCoronaTempTables..test_bmi_codes FROM (
SELECT Patient_ID, CTV3Code, NumericValue, ConsultationDate
FROM CodedEvent
WHERE CTV3Code IN ('X76C7','22A..', 'XM01E','229..', '22K..')
) t
#!/bin/bash
set -euo pipefail
help_text="
Usage: bin/development-services [-h|--help] [-n|--no-attach]
Executes the commands listed in the 'Procfile.development' file, each in its
own pane of a new tmux session, and then attaches to that session.
If an appropriate tmux session already exists then we attach to it without
@evansd
evansd / postinstall.sh
Created June 28, 2011 21:30
postinstall sript from vagrant example box (having it somewhere web-accessible is easiest way to get it on to newly created VMs)
# Apt-install various things necessary for Ruby, guest additions,
# etc., and remove optional things to trim down the machine.
apt-get -y update
apt-get -y remove apparmor
apt-get -y install linux-headers-$(uname -r) build-essential
apt-get -y install zlib1g zlib1g-dev libxml2 libxml2-dev libxslt-dev libssl-dev openssl libreadline5-dev
apt-get clean
# Remove this file to avoid dhclient issues with networking
rm -f /etc/udev/rules.d/70-persistent-net.rules
#!/bin/bash
set -eo pipefail
this_dir="$( unset CDPATH && cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${GIT_DIR="$this_dir/shared-git-repo"}
remote="$1"
sha="$2"
local_path="$3"
@evansd
evansd / defer_signals.py
Last active March 31, 2020 15:39
Context manager to defer signal handling until context exits
# The MIT License (MIT)
#
# Copyright (c) 2013 David Evans
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions: