Skip to content

Instantly share code, notes, and snippets.

@5263
5263 / ultimo.py
Created June 14, 2018 18:18
Last day of previous months
import datetime,calendar
def ultimo(num=3,today=None):
"""return a list of datetime.date with the last day of num previous months"""
if today is None:
today = datetime.date.today()
return [datetime.date(y,m,calendar.monthrange(y,m)[1]) for \
m,y in (((today.month - i -1) % 12+1, today.year + \
((today.month - i -1) // 12)) for i in range(num+1,0,-1))]
@5263
5263 / checkoutandload.FCMacro
Last active October 26, 2020 19:28
FreeCAD files git repo
#!/usr/bin/env python
import zip2git
from PySide import QtGui
# 1. Choose repository (directiory)
repodir = QtGui.QFileDialog.getExistingDirectory(QtGui.qApp.activeWindow())
if repodir:
# ToDO: 2. List branches in a list and let the user choose one
# ToDo: 3. List commits in a list and let the user choose one
# ToDo: 4. Check if it would overwrite a file. And if the file is
# part of the repo. Ask the user for cofirmation
@5263
5263 / parsegitweb3.py
Last active April 24, 2021 16:33
gitwebparser
#!/usr/bin/env python3
import bs4
import requests
sa = requests.Session()
sa.verify = False # workaround
# sa.auth = BearerAuth()
sa.timeout = 10
@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 / 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 / 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 / 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 / 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 / exrsdoc.py
Last active August 5, 2016 10:57
parser for the rdsoc file format
#!/usr/bin/env python
import zipfile
tempdirlin='/home/user/sattemp'
tempdirwine='D:\\'
converterbin="/windows/Programme/Program Files (x86)/DesignSpark/DesignSpark Mechanical 1.0/sabSatConverter.exe"
# add freecad libdir to path
import sys
sys.path.insert(0,'/usr/local/freecad/lib')
@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())