Skip to content

Instantly share code, notes, and snippets.

Non-blocking

The System.lang.Runtime.exec() method is non-blocking, so you call it and it spawns a background process.

Unfortunately the java.lang.Process thingy in Java 8 has no way to get a PID, so you need a cheesy hack to find it.

@wware
wware / connect_two_points.scad
Last active August 3, 2018 19:50
OpenSCAD code to create a cylinder connecting two given points in 3-space
v1 = [0, 2, -1];
v2 = [1, 3, 3];
y = [v2[0] - v1[0], v2[1] - v1[1], v2[2] - v1[2]];
translate(v1)
rotate(-acos(y[2] / norm(y)), cross(y, [0, 0, 1]))
cylinder(h=norm(y), d=0.1);
#!/usr/bin/env python
# https://en.wikipedia.org/wiki/Isomorphic_keyboard
# any hexagonal layout is defined by two offsets (the value of the third is forced by the first two)
import sys
many = [20 * [None] for _ in range(20)]
def display():
width = 4

Messing with Ansible and Docker

Nobody can remember how to use Docker, it is far too confusing. So here are some hints.

sudo docker build -t simple_flask:dockerfile .
sudo docker run -p 5000:5000 simple_flask:dockerfile python hello.py

My actual intention is to learn Ansible. But in order to run Ansible you need Docker, unless you happen to have a rackful of machines at your disposal which I do not.

@wware
wware / bash_waiter.sh
Created February 22, 2018 23:36
I want to run a couple Bash processes in parallel in the background. If I use "wait", I might end up waiting for a long-running process to finish after another process has already failed, and in this case I want to stop as soon as I see the failure.
#!/bin/bash
wait_all() {
# As soon as one of the background processes creates the EPIC_FAIL file,
# kill all background processes, rm EPIC_FAIL, and stop looping.
while [ $(jobs | grep -v Done | wc -l) -ne 0 ]; do
sleep 1
if [ -f EPIC_FAIL ]; then
rm -f EPIC_FAIL
echo OUCH
#!/usr/bin/env python
import argparse
import logging
import os
import sys
import pprint
from textwrap import dedent
HERE = os.path.dirname(__file__)
#!/usr/bin/env python
import logging
from flask import Flask
from twisted.internet import ssl, reactor
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site
from twisted.python.threadpool import ThreadPool
from twisted.application import service

Feature toggle decorator

This is a feature toggle scheme for the system I maintain at my job. The goal is to be able to easily toggle functions that have been recently pushed to production, so if things go bad, we have an easy mitigation plan.

Create a database table with three columns, Key (string), Value (boolean), LastChanged (timestamp). The purpose of the LastChanged column is just for record keeping to decide when a feature is deemed trustworthy. The several for a given key string present the history of switching the feature on and off,

@wware
wware / log_caller_frame.py
Last active November 28, 2017 19:51
Sometimes you want a Python function that includes a `logging.info()` or `logging.debug()` call, but you'd really prefer the logged filename and line number to be from the caller of your function, not your function itself. This is the solution to that problem, at least for Python 2.7. Also see https://stackoverflow.com/questions/12980512/custom-…
import os
import logging
from contextlib import contextmanager
logging.basicConfig(
format='%(asctime)-15s %(levelname)s %(filename)s:%(lineno)d %(message)s',
level=logging.INFO
)
def __LINE__():
@wware
wware / bash_without_getopt_s.sh
Last active February 1, 2018 21:31
Apparently getopt(1) for Bash is buggy, according to http://mywiki.wooledge.org/BashFAQ/035#getopts, where getopts is OK. But it's not that hard to just write scripts that don't use either of them.
#!/bin/bash
help() {
echo "Bash script that parses cmd line args without getopt(s)"
echo "Adapted from https://stackoverflow.com/questions/192249"
echo " -h, --help print this help message"
echo " -f, --foo set FOO variable"
echo " -b, --bar set BAR variable"
echo " -d, --debug enable debug (doesn't really do anything here)"
echo " <anything else> becomes a positional argument"