Skip to content

Instantly share code, notes, and snippets.

Jasmijn Wellner gvx

View GitHub Profile
gvx /
Created May 18, 2020
metadecorators to construct decorators that only transform function arguments or return values
from functools import wraps, partial
def transform_return(transformation, decorated=None):
if decorated is None:
return partial(transform_return, transformation)
def wrapper(*args, **kwargs):
return transformation(decorated(*args, **kwargs))
return wrapper
gvx /
Created May 15, 2020
simple argparse wrapper
from inspect import signature, Signature, Parameter
from argparse import ArgumentParser
def parse_args(parser, argv=None):
return parser.parse_args(argv).__dict__
class Command:
def __init__(self, f, parser):
gvx /
Last active Mar 17, 2020 — forked from dotchetter/
Call functions through the PollCache object for only receiving output from the function if new output is detected.
# disclaimer: untested code ahead! see
from dataclasses import dataclass
from enum import Enum, auto
from typing import Any, Callable, Tuple
class CacheValue:
result: Any
times_changed: int = 1
gvx /
Created Feb 1, 2019
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):
class Curried:
f: Callable
def __call__(self, *args, **kwargs):
f = partial(self.f, *args, **kwargs)
except TypeError:
return type(self)(f)
gvx /
Last active May 13, 2020
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
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
#!/usr/bin/env python
# coding=utf-8
# <a class="btn btn-default pull-right" href=""><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 / output
Last active Aug 29, 2015
named tuple performance
View output
$ py3.3
default implementation:
my implementation:
$ py3.3
default implementation:
my implementation:
gvx /
Created Dec 11, 2014
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)')
You can’t perform that action at this time.