Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/python
import serial
import sys
import select
import os
ser=serial.Serial('/dev/ttyUSB0',115200)
BUF=4096
@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
"""
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 / 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.

@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 / 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
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);

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.

@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))