Skip to content

Instantly share code, notes, and snippets.

@wware
wware / closures.py
Created October 16, 2014 15:51
Python closures and decorators - really broken, or simply horrible?
from functools import wraps
# A decorator without an argument.
def add_four(f):
@wraps(f)
def wrapped(x):
y = f(x)
return y + 4
return wrapped
@wware
wware / Sierpinski.md
Last active August 29, 2015 14:08
Parameterized Sierpinski tetrahedron. It would be sweet if this were suitable for metal casting.

OpenSCAD on the Linux box is faster than on the Macbook, I think, but it won't do recursion. Recursion must be done in Python. So the Sierpinski code must be rewritten as a Python script or an exercise in SolidPython that generates OpenSCAD.

The Linux version of OpenSCAD has a few other differences. Cylinders (and possibly spheres) don't support diameter arguments, only radius arguments. Anyway, I'm adding a Python script to perform the recursion and produce a flat *.scad file.

Or instead of all this nonsense resulting from the fact that the OpenSCAD in the Ubuntu repository is old and broken, you could just go to the download page and get a version that works correctly.

class LinearRegression:
"""
>>> lr = LinearRegression()
>>> lst = [(0., 0.)]
>>> lr.add(lst[0][0], lst[0][1])
>>> lr.get()
>>> lr.error()
>>> for nxt in [(1., 1.), (2., 2.), (4., 3.), (-1., -0.5)]:
... lst.append(nxt)
... lr.add(nxt[0], nxt[1])
@wware
wware / swish-conf-builder.py
Created November 14, 2014 13:20
This helps to build a swish.conf file for use with swish-e, which can then help to quickly search a large project for keywords.
#!/usr/bin/env python
"""
Example usage:
find . -type f | head -3000 | ./suffixes.py
Use this to build a swish.conf file for swish-e, e.g:
IndexDir .
IndexOnly .h .qml .cpp .png .pro .qmlproject .svg .jpg .qdoc .qrc .json .js .sci .xml .pri .desktop .txt
IndexContents TXT* .h .qml .cpp .png .pro .qmlproject .svg .jpg .qdoc .qrc .json .js .sci .xml .pri .desktop .txt
"""
#!/usr/bin/python
import serial
import sys
import select
import os
ser=serial.Serial('/dev/ttyUSB0',115200)
BUF=4096
@wware
wware / about.py
Last active June 2, 2016 18:30
Yet another attempt to write a useful var_dump for Python
import pprint
import logging
def about(thang):
def shorty(x):
if len(x) > 200:
x = x[:200] + "..."
return x
if isinstance(thang, dict):
@wware
wware / Git-Merge-Lore.md
Last active August 29, 2015 14:17
Tricks and tips for "git merge"

Git Merge Lore

I'm working at a place where people prefer merges over rebases. In the past I've been influenced by the pro-rebase school of thought, but if there are relatively few pull requests being merged, you can use --no-ff to maintain pretty good clarity, and merge points become the decision point where a pull request has been included or excluded. With that in mind, I want to look at a few relevant use cases.

@wware
wware / gen_shape.py
Last active August 29, 2015 14:21
Do some linear algebra and generate a 3D shape. Kind of a skewed truncated pyramid.
import doctest
import numpy
import sys
def is_vector(x):
return isinstance(x, numpy.ndarray) and x.shape == (3,)
def normalize(vec):
@wware
wware / synth.py
Last active August 29, 2015 14:25
import aifc
import math
import sys
filename = sys.argv[1]
F_SAMPLE = 40000
DT = 1. / F_SAMPLE
class Aiff: