Skip to content

Instantly share code, notes, and snippets.

View secnot's full-sized avatar

secnot

View GitHub Profile
@secnot
secnot / sort.py
Last active April 29, 2018 23:16
Sort object list by one or multiple attributes in normal or reverse order in python3
from operator import attrgetter
def sort_by_attr(alist, *args):
"""
Sort an object list by one or more of its atributes
Parameters:
alist (list): object list
*args (strings): the name of one or more of the objects attributes
possibly preceded by a '-' sign to use reversed order
@secnot
secnot / plot.py
Created April 23, 2018 12:41
Plotting rectpack results using matplotlib
# For more details: http://matthiaseisen.com/pp/patterns/p0203/
for index, abin in enumerate(packer):
bw, bh = abin.width, abin.height
print('bin', bw, bh, "nr of rectangles in bin", len(abin))
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
for rect in abin:
x, y, w, h = rect.x, rect.y, rect.width, rect.height
plt.axis([0,bw,0,bh])
@secnot
secnot / go_test_coverage.sh
Last active June 18, 2017 10:30
Generate Go package test coverage
$ go get -u github.com/username/repo
# Profile test
$ go test -coverprofile cover.out github.com/username/repo
# Generate nicer html representation from profiles
$ go tool cover -html=cover.out -o cover.html
@secnot
secnot / digit.py
Created February 21, 2017 22:19
Get the nth digit of an integer
def digit(num, n):
"""Returns nth digit of num (both integers)"""
return abs(num)//10**n%10