Skip to content

Instantly share code, notes, and snippets.

@gvx
gvx / py2md.py
Last active February 1, 2017 20:12 — forked from AtHeartEngineer/py2md.py
#!/usr/bin/env python
# coding=utf-8
# <a class="btn btn-default pull-right" href="https://gist.github.com/TylerShaw/48ead56c19ce905ac513"><i class="fa fa-git"></i> Download the gist here!</a>
# Py2Md started as a little project to do the magical "self documenting code". After thinking about it, I realized self documenting code is great, but it's really not the point.
# Commenting code properly, if only better, is the point.
# This script evolved from me wanting to code better. I often look at other peoples code, or even old code I've written, and it takes me a few minutes to even figure out what each section is doing.
# This will hopefully solve that, not only by forcing me to comment code better, but to publish my code with decent comments.
# This script reads in a python file (either itself, or anything it's
# imported into) and converts the python file into a markdown file. It
@gvx
gvx / output
Last active August 29, 2015 14:11
named tuple performance
$ py3.3 time_them.py
default implementation:
2.1573487099958584
my implementation:
0.7448599760100478
$ py3.3 time_them_2.py
default implementation:
0.6372028530022362
my implementation:
0.20809232600731775
@gvx
gvx / named.py
Created December 11, 2014 23:28
An alternative namedtuple
from collections import OrderedDict
from inspect import Parameter, signature
from itertools import chain, starmap
from operator import itemgetter
__all__ = ['namedtuple']
dict_property = property(lambda self: OrderedDict(zip(self._fields, self)),
doc='dictionary for instance variables (if defined)')
@gvx
gvx / keybase.md
Created September 27, 2014 15:01

Keybase proof

I hereby claim:

  • I am gvx on github.
  • I am robinwell (https://keybase.io/robinwell) on keybase.
  • I have a public key whose fingerprint is 43E3 C73A 188A 844E 39F1 EEBA E429 16EB 3793 DA2D

To claim this, I am signing this object:

@gvx
gvx / classes.lua
Created July 26, 2014 19:34
A simple, alternative class system for Lua
return function(name)
local class = require(name)
local classmt = getmetatable(class)
if getmetatable(class) == nil then
local props = {unpack(class)}
for i = #class, 1, -1 do
class[i] = nil
end
local classmt = {}
local instancemt = {__index = class}
@gvx
gvx / Easy output
Created July 16, 2014 22:06
Daily Programmer #171 Easy/Intermediate
xxxxxxxx
x x
x xxxx x
x x x x
x x x x
x xxxx x
x x
xxxxxxxx
---
@gvx
gvx / gist:9765299
Created March 25, 2014 16:15
2 woorden 9 letters
from random import choice
with open('/usr/share/dict/nederlands') as f:
words = f.read().split('\n')
def get_random_pair():
w1 = choice(words)
w2 = choice(words)
return len(w1) + len(w2), w1, w2
class LinkedList:
next = None
val = None
def __init__(self, val):
self.val = val
def add(self, val):
if self.next is None:
self.next = LinkedList(val)
@gvx
gvx / roundrect.lua
Created February 18, 2014 15:13
Rounded rectangles
function love.graphics.roundrect(mode, x, y, width, height, xround, yround)
local points = {}
local precision = (xround + yround) * .1
local tI, hP = table.insert, .5*math.pi
if xround > width*.5 then xround = width*.5 end
if yround > height*.5 then yround = height*.5 end
local X1, Y1, X2, Y2 = x + xround, y + yround, x + width - xround, y + height - yround
local sin, cos = math.sin, math.cos
for i = 0, precision do
local a = (i/precision-1)*hP
@gvx
gvx / gist:5696885
Created June 3, 2013 08:35
Extract page numbers that haven't been included in index.txt yet
last = None
with open('index.txt') as f:
for line in f:
new = int(line.decode('utf-8').split('\t', 1)[0])
if last is not None:
if last - new > 1:
if last - new == 2:
print new + 1
else: