Skip to content

Instantly share code, notes, and snippets.

@wware
wware / debuggy_thinger.py
Last active December 15, 2015 22:58
Handy debug thing for logging the entry and exit of functions and methods, showing arguments and return values. I found this useful for seeing how things work in an unfamiliar library (in this case, django-haystack).
import logging
import sys
import types
# It's great to log the entering/leaving of methods and functions, but it would be way cool
# to see who gets called by the function you go into. That probably means hacking ASTs and
# finding the GOSUB bytecode.
def discover(obj, filtfunc=None):
if filtfunc is None:
@wware
wware / grok_mock.py
Last active May 18, 2021 15:18
I find mock confusing and mystical. Here is some clarification.
"""
Mock is confusing as hell. Document some of it.
http://www.voidspace.org.uk/python/mock/patch.html
http://mock.readthedocs.org/en/latest/index.html
"""
import unittest
import pprint
from mock import MagicMock, patch, call
@wware
wware / symbol.ps
Last active December 22, 2015 10:08
Postscript code to make a cleaner version of the vacuum tube artist-previously-known-as symbol
% http://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF
/xcenter 306 def
/ycenter 500 def
/scalefactor 8 def
/scale1 {
scalefactor mul
} def
@wware
wware / JOINs_in_MySQL.sql
Last active August 29, 2015 13:58
I never remember this JOIN database stuff unless I have a reminder like this.
-- These examples are from http://en.wikipedia.org/wiki/Join_(SQL)
DROP DATABASE IF EXISTS dumb_example;
CREATE DATABASE dumb_example;
USE dumb_example;
CREATE TABLE department
(
DepartmentID INT,
DepartmentName VARCHAR(20)
# Tail recursion in Python
# http://stackoverflow.com/questions/13591970/does-python-optimize-tail-recursion
# As far as the question, does Python inherently support tail recursion?
# No, and it never will since Guido prefers to be able to have proper tracebacks
# http://neopythonic.blogspot.com.au/2009/04/tail-recursion-elimination.html
# http://neopythonic.blogspot.com.au/2009/04/final-words-on-tail-calls.html
# But it's possible to do it with a decorator, and the stack never gets too deep.
# http://code.activestate.com/recipes/496691-new-tail-recursion-decorator/
@wware
wware / intersect_circles.py
Last active July 26, 2023 17:42
Intersection of two circles in two dimensions
from math import cos, sin, atan2
class Circle:
def __init__(self, x, y, radius):
self.x, self.y, self.radius = x, y, radius
def intersect_circles(first, second):
"""
>>> intersect_circles(Circle(0, 0, 1), Circle(1, 1, 1))
((1.5700924586837752e-16, 1.0), (1.0, 0.0))

Fun hacks with OpenSCAD

Hack the First

Use OpenSCAD to take a slice from a 3D object and convert it to a 2D DXF file, suitable for laser cutting.

module home_depot_bucket() {
translate([0,0,14.25])
difference() {
cylinder(h=1/4, d=14, $fn=30);
cylinder(h=1/4, d=12, $fn=30);
}
difference() {
cylinder(h=14.5, d1=10.5, d2=12.5, $fn=30);
translate([0,0,1/4])
cylinder(h=14.5, d1=10, d2=12, $fn=30);
@wware
wware / then.py
Created October 1, 2014 21:41
Can we do a JavaScript-like .then() hack in Python?
# Can we do a JavaScript-like .then() hack in Python?
import threading
import time
class Sleeper(threading.Thread):
def __init__(self):
super(Sleeper, self).__init__()
self.f = None