Skip to content

Instantly share code, notes, and snippets.

@wware
wware / papertrail.py
Last active August 9, 2016 19:50
You can write decorators for classes! Who knew? This is a decorator to track the history of what happens to instances of the decorated class following the initial call to their constructor.
import types
import inspect
import pprint
from collections import defaultdict
def get_stack(offset=0):
def frame2tuple(frame):
info = inspect.getframeinfo(frame[0])
return (info.filename, info.lineno)
@wware
wware / keypuncher.ino
Created August 18, 2016 20:42
Teensy program to keep your computer awake (and therefore your VPN). Compile with Tools->USB->Keyboard.
int count = 0;
void setup() { }
void loop() {
int i;
Keyboard.print("Hello World ");
Keyboard.print(count);
count = count + 1;
delay(2000);
for (i = 0; i < 20; i++) {
Keyboard.print("\177");
#!/usr/bin/env python
import os
import re
import time
import subprocess
os.system("ps ax | grep fluidsynth | cut -c -6 | xargs kill -9")
devnull = os.open('/dev/null', os.O_WRONLY)

I need a reminder about how to set up a service in Linux that will run a script at boot time. Put this LSB init script in your /etc/init.d directory and then type

sudo update-rc.d linux_service.sh defaults

to set up the service. This is a wrapper for the script you actually want to run. The "Required-Start" section allows you to choose what Linux services need to be running before your own service is started. (I think my Fluidsynth script probably wants "alsasound".)

Some relevant links about this stuff:

The Magic of SQLACodeGen

SQLACodeGen is a work of genius. Here is the command you use to generate the model classses for your database. Lately I'm working with MySQL, previously I was working with PostgreSQL.

sqlacodegen mysql://user:password@server/database > mymodels.py

Had I been aware of SQLACodeGen three months ago, I would have used SQLAlchemy for a project where instead I wrote a lot of raw SQL.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import random
import os
from Crypto.Cipher import AES
def _make_binary(x):
for c in list(x):
assert c in '0123456789ABCDEFabcdef'
@wware
wware / 0README.python-swig
Last active November 28, 2016 15:58
Reminder of how to use SWIG to wrap C functions for use in Python
Do this:
make
gdb -x hook.x `which python`
More info: http://www.swig.org/Doc1.3/Python.html
@wware
wware / mem.py
Created March 16, 2017 03:33
On my job, I need to debug what appears to be a memory leak in a Python program that spins up a bunch of workers using the multiprocessing module and then it uses managed queues to send them work and get back results. But the memory is going crazy. This is my attempt to better wrap my head around what Python does with memory.
# A litle library of memory diagnostics
# In no particular order of usefulness:
# https://pymotw.com/2/gc/
# https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609
# https://www.huyng.com/posts/python-performance-analysis
# http://stackoverflow.com/questions/23369937
import gc
import os