Skip to content

Instantly share code, notes, and snippets.

@5263
5263 / diffblame.py
Created February 1, 2014 13:42
git blame on git diff results
#!/usr/bin/env python
"""usage: diffblame.py START..END [path]"""
import os,sys,subprocess
gitpath = subprocess.Popen(["git", "rev-parse","--show-toplevel"],\
stdout=subprocess.PIPE).communicate()[0].strip()
diff = subprocess.Popen(["git", "diff","-U0"]+sys.argv[1:],\
stdout=subprocess.PIPE).communicate()[0]
for l in diff.splitlines():
if l=='':
@5263
5263 / wire2bez.py
Last active August 29, 2015 13:56
FreeCAD Macro to convert a wire into a piecevise Bezier curve
import FreeCAD
def segments2points(segments,degree):
pts=segments[0].getPoles()[0:1]
for seg in segments:
if seg.Degree < degree:
seg.Increase(degree)
pts.extend(seg.getPoles()[1:])
return pts
@5263
5263 / mink2offset.py
Created February 15, 2014 14:03
Convert FreeCAD Minkowski Sum Features to Offset Features
import FreeCAD,OpenSCADFeatures
for obj in FreeCAD.ActiveDocument.Objects:
if obj.TypeId == "Part::FeaturePython" and hasattr(obj,'Proxy') and isinstance(obj.Proxy,OpenSCADFeatures.CGALFeature) \
and len(obj.Children) == 2:
print [(child.TypeId,child.PropertiesList) for child in obj.Children]
if obj.Children[0].TypeId in ['Part::Cylinder','Part::Sphere']:
OpenSCADFeatures.OffsetShape(obj,obj.Children[1],float(obj.Children[0].Radius))
obj.touch()
elif obj.Children[1].TypeId in ['Part::Cylinder','Part::Sphere']:
OpenSCADFeatures.OffsetShape(obj,obj.Children[0],float(obj.Children[1].Radius))
@5263
5263 / hexplacment.py
Created February 20, 2014 11:53
exact representation of a FreeCAD placement
def hexplacement(plm):
fhex="*[float.fromhex(s) for s in %s]"
fstr="FreeCAD.Placement(FreeCAD.Vector(%s),FreeCAD.Rotation(%s))"
t=[f.hex() for f in plm.Base]
r=[f.hex() for f in plm.Rotation.Q]
return fstr % (fhex % t, fhex % r )
if __name__=='__main__':
import FreeCAD
print hexplacement(FreeCAD.Placement())
@5263
5263 / uniname.py
Created September 25, 2014 09:13
encode unicode with name literals
import unicodedata
def encodechar(c):
i=ord(c)
if i >= 32 and i <=122 and \
c not in '\'\"/\\^_~`|':
return c
else:
if not isinstance(c,unicode):
c=unicode(c)
name = unicodedata.name(c,None)
@5263
5263 / FreeFormTurning.py
Last active August 29, 2015 14:07
Free form turning
import FreeCAD
import Part
def rayshape(phi=0,z=0,r=1e3):
import math
x=r*math.cos(phi)
y=r*math.sin(phi)
l=Part.Line(FreeCAD.Vector(0,0,z),FreeCAD.Vector(x,y,z))
return l.toShape()
@5263
5263 / phpbbpost.py
Last active August 29, 2015 14:07
post on PHPBB forum
def phppost(subject,message,url,forum,username,password):
import mechanize
import time
br = mechanize.Browser()
br.open(url+'/ucp.php?mode=login')
loginform = tuple(br.forms())[1]
br.form=loginform
loginform.find_control("username").value=username
loginform.find_control("password").value=password
loginform.find_control("viewonline").set_single(True)
@5263
5263 / hash.py
Last active August 29, 2015 14:07
blobhash recursive
#!/usr/bin/env python
def blobhash(str1):
import hashlib
hash1=hashlib.sha1("blob %d\0" % len(str1))
hash1.update(str1)
return hash1.hexdigest()
def walk(dir1,extension=".py"):
import os
for root, dirs, files in os.walk(dir1):
@5263
5263 / checkwire.py
Last active August 29, 2015 14:07
check if a FreeCAD wire can be traversed
import FreeCAD
def checkwire(w):
"""steps through every edge of a wire and checks the orientation"""
pos=w.Vertexes[0].Point
for i,e in enumerate(w.Edges):
if e.Orientation == "Forward":
vs=e.Vertexes
else:
vs=e.Vertexes[::-1]
@5263
5263 / gist:1209263
Last active September 27, 2015 04:08
Calc Checksums for ean (isbn13) and isbn10
def isbn13t10(str1):
if len(str1)==13 and str1.startswith('978') and \
str1[12]==checksum(str1[0:12]):
return '%s%s' %(str1[3:12],isbnchecksum(str1[3:12]))
def isbn10t13(str1):
if len(str1)==10 and \
str1[9].upper()==isbnchecksum(str1[0:9]):
ean12='978%s'% str1[0:9]
return '%s%s' % (ean12,checksum(ean12))