Skip to content

Instantly share code, notes, and snippets.

View gvx's full-sized avatar

Jasmijn Wellner gvx

  • None
  • The Netherlands
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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
import threading
from time import perf_counter as time
import random
from statistics import median, stdev
results = {'thread': [], 'call': []}
end = 0
def inside():
global end
@gvx
gvx / find_for_else.py
Last active February 15, 2021 10:21
Find all occurrences of for ... else
import ast
from pathlib import Path
class Visitor(ast.NodeVisitor):
def __init__(self):
self.for_found = 0
self.for_else_found = 0
self.while_found = 0
self.while_else_found = 0
self.files_tried = 0
@dataclass
class Curried:
f: Callable
def __call__(self, *args, **kwargs):
f = partial(self.f, *args, **kwargs)
try:
signature(f).bind()
except TypeError:
return type(self)(f)
else:
@gvx
gvx / halfmutable.py
Created February 1, 2019 21:33
Generalised hashable types in Python
from dataclasses import dataclass, field, fields, MISSING
def immutable_field(*, default=MISSING, default_factory=MISSING):
return field(default=default, default_factory=default_factory, metadata={'frozen': True})
def mutable_field(*, default=MISSING, default_factory=MISSING):
return field(default=default, default_factory=default_factory, compare=False)
def halfmutable(_cls=None, *, init=True, repr=True, order=False):
def wrap(cls):