Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
thomasballinger / subprocess.py
Created December 15, 2013 23:26
Using a pseudo-terminal to interact with interactive Python in a subprocess
View subprocess.py
from subprocess import Popen, PIPE
import pty
import os
from select import select
import sys
import tty
master, slave = pty.openpty()
p = Popen(['python'], stdin=slave, stdout=PIPE, stderr=PIPE)
pin = os.fdopen(master, 'w')
@thomasballinger
thomasballinger / gist:5879705
Created June 27, 2013 19:44
Iterative and recursive minimax
View gist:5879705
class Board(object):
"""
>>> Board().rows
((' ', ' ', ' '), (' ', ' ', ' '), (' ', ' ', ' '))
>>> print Board()
| |
-----
| |
-----
@thomasballinger
thomasballinger / gist:1336916
Created November 3, 2011 16:13
nifti2nrrd dwi conversion
View gist:1336916
#!/usr/bin/env python
#TODO
#1) Add measurement frame
#2) Save data part of nifti to its own file, and point to it in 'data file' field
import nibabel as nib
import sys
@thomasballinger
thomasballinger / vtk2vtp.py
Created October 12, 2011 15:10
vtk2vtp.py
View vtk2vtp.py
#!/usr/bin/env python
"""File format conversion
category: vtk, file conversion, tomb"""
import os, sys
import vtk
def vtk2vtp(invtkfile, outvtpfile, binary=False):
"""What it says on the label"""
reader = vtk.vtkPolyDataReader()
View gist:2991211
#!/usr/bin/env python
"""An externally-accessible toy server whith several response strategies"""
import socket
import sys
from multiprocessing import Pool
def web_scrape(site):
s = socket.socket()
s.connect((site, 1333))
View myrepl.mjs
import tty from 'tty'
import { emitKeypressEvents } from 'readline';
let drain;
process.stdout.on('drain', () => { drain() });
const waitForDrain = () => new Promise(r => {drain = r});
function waitForDrainify(func) {
return async (...args) => {
if (!func(...args)) {
await waitForDrain();
View endless-sky emscripten progress
# Starting with Ubuntu 18.04.3 (LTS) x64
# I'm using a local VMwareFusion vm instead of Docker or something because I want to be able to test with a video device.
# New install
# (not shown) make account, enable ssh, add vm to /etc/hosts
ssh vm
apt-get update
sudo apt install git
git clone https://github.com/endless-sky/endless-sky.git
@thomasballinger
thomasballinger / cell_invalidation.ipynb
Created February 1, 2020 08:15
sketch of noticing that a cell has been removed or reexecuted. Useful for stopping timers, removing event listeners, etc.
View cell_invalidation.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@thomasballinger
thomasballinger / invalidation.js
Created February 5, 2020 03:40
invalidation of Jupyter notebook cells
View invalidation.js
// Find deepest DOM ancestor that does not change on reexecute
const findParentOutputDiv = (el) => {
let candidate = el;
while (candidate) {
candidate = candidate.parentElement
if (candidate.className === 'output') {
return candidate;
}
}
throw Error("parent output div not found");
View server.py
import socket
import select
import sys
clients = []
server = socket.socket()
server.bind(('', 8001))
server.listen(1)