Skip to content

Instantly share code, notes, and snippets.

@oconnor663
oconnor663 / gist:b7987d336d99d6a17dfe
Created May 22, 2015 17:30
Go's weird treatment of newlines
package main
import "fmt"
type Foo struct {
Bar int
}
func main() {
var myfoo Foo
@oconnor663
oconnor663 / gist:aa27fe8cf01e05d1d759
Created July 8, 2015 20:17
Equivalent key with different bytes in PyNaCl
import nacl.utils
import nacl.public
sender_key = nacl.public.PrivateKey.generate()
recipient_key_1 = nacl.public.PrivateKey.generate()
# Construct the second recipient by changing one bit from the first.
key_bytes = bytearray(recipient_key_1.encode())
key_bytes[0] ^= 1
recipient_key_2 = nacl.public.PrivateKey(bytes(key_bytes))
@oconnor663
oconnor663 / build_debian.py
Last active August 29, 2015 14:27
Build script comparison
#! /usr/bin/env python3
from pathlib import Path
from subprocess import run, PIPE
from sys import argv
from tempfile import mkdtemp
here = Path(__file__).parent
build_root = Path(argv[1] if len(argv) >= 2 else mkdtemp())
@oconnor663
oconnor663 / install.py
Created August 16, 2015 05:10
dotfiles install script comparison
#! /usr/bin/env python3
import os
from pathlib import Path
from shutil import which
from subprocess import run
import sys
here = Path(__file__).parent.resolve()
os.chdir(str(here))
@oconnor663
oconnor663 / libertarians_in_airplanes.md
Last active August 27, 2015 11:54
Libertarians in Airplanes

A response to http://www.ginandtacos.com/2008/08/31/atheistsfoxholes-libertariansairplanes/

First and foremost, this post is about the author's experiences talking to a bunch of libertarians at AEI. It sounds like those folks were dismissive of important left-leaning arguments, and generally annoying to be around. That's a shame, and no amount of the-free-market-being-awesome is going to make the author's experience anything other than a bad memory.

So with the caveat that I'd always want to acknowledge that up front before I started playing with ideas, here goes. There's a very common mistake that pro-regulation people make when they think about markets that don't exist yet. The steps go something like this.

  1. consider the way something works right now
  2. change one part of it to resemble a market
  3. observe that it wouldn't work very well
@oconnor663
oconnor663 / example.py
Last active August 26, 2015 03:31
run asyncio without disrupting other code?
import asyncio
import contextlib
@asyncio.coroutine
def my_coroutine(loop, text):
p = yield from asyncio.create_subprocess_shell("echo " + text, loop=loop)
yield from p.communicate()
@oconnor663
oconnor663 / rw.py
Created September 2, 2015 00:17
reading and writing from an os.pipe() in asyncio
#! /usr/bin/python
import asyncio
import os
@asyncio.coroutine
def do_writing(writer):
for i in range(1, 4):
writer.write(("stuff " + str(i)).encode())
@oconnor663
oconnor663 / osx_hang.py
Created September 5, 2015 00:24
Why does this hang on Mac but not on Linux
import os
import subprocess
r, w = os.pipe()
# Small enough to fit in the pipe buffer; won't hang.
os.write(w, b"one\ntwo\n")
# Call head twice. The first time works fine. The second hangs, but only on OSX!
subprocess.call(['head', '-c', '4'], stdin=r)
@oconnor663
oconnor663 / kourier_expire_times.iced
Created September 25, 2015 04:18
importing a key with time_travel=true changes the computed expiration time
kbpgp = require 'kbpgp'
armored = """-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1
mQINBFKB8x0BEADeCf76s/2pjkRKhJCGw3WeeARvMGxCInAbrQ1wHtdCBawBFdrD
oYDOvVX0vgWS+dIRv/UV7djtvji4K19tRRd79L/9BmFUfW/73va8dAyXEu9/RJIQ
+1+ku+pl8+YZ3n1ZyNRhtBMIu5QDUqf355FaIE1E6uaS1gjjUdSsWjc0CN4d1Vag
Bi/ImES4ru8+xLuePkVil4vL/3TOHQ4qNnInD2bR6pi6WSwZNUJsPeSG8RuX2GC6
a0Ou8glL53YvIW+JtY44nVbJUw824/gQZQTzVsUJTyuuvQaIRD6JzGFQHcLXa9/3
@oconnor663
oconnor663 / subprocess_deadlock_demo.py
Last active December 2, 2015 15:23
demonstration of a Windows deadlock in subprocess.py, with extra sleeps to trigger it
#! /usr/bin/env python3
# This is a python version of this pipeline: `echo foo | cat`. It demonstrates
# a deadlock that can happen on Windows when Popen is called simultaneously
# from two different threads. Although os.pipe() file descriptors are not
# normally inheritable (never on Windows, and not in recent Python versions on
# POSIX), the Windows implementation of Popen() creates temporary inheritable
# copies of its descriptors. See _make_inheritable() in subprocess.py. If a
# second process is spawned while those inheritable copies are open, both
# children will inherit the copies. With pipes, this can cause deadlocks when