Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
vxgmichel / twindragons.py
Created February 2, 2017 15:38
Generate a twin dragon fractal using pygame
import pygame
import random
import imageio
from fractions import gcd
lcm = lambda x, y: x * y // gcd(x, y)
def random_color():
r = lambda: random.randint(0, 255)
@vxgmichel
vxgmichel / aiogo.py
Last active July 5, 2017 14:57
Go-style generators in asyncio
import asyncio
from functools import wraps
def gogenerator(aiterable=None, buffering=0):
def decorator(aiterable):
@wraps(aiterable)
def wrapper(*args, **kwargs):
return go(aiterable(*args, **kwargs), buffering)
return wrapper
@vxgmichel
vxgmichel / go-style-generators.md
Last active July 26, 2023 06:50
Go-style generators in asyncio

Go-style generators in asyncio

I've recently been looking into the go concurrency model to see how it compares to asyncio.

An interesting concept caught my attention: go generators.

Generators in Go

@vxgmichel
vxgmichel / protect.py
Created September 29, 2017 16:30
Protecting against SIGINT using ThreadPoolExecutor
# Common imports
from time import sleep
# Synchronous imports
from concurrent.futures import ThreadPoolExecutor
# Asynchronous imports
# from gevent.threadpool import ThreadPoolExecutor
@vxgmichel
vxgmichel / isolate.py
Created October 25, 2017 15:49
Isolate an asynchronous generator by running it in a background task
import asyncio
from itertools import count
from collections import AsyncIterable
async def clock(start=0, step=1, interval=1.):
for i in count(start, step):
yield i
await asyncio.sleep(interval)
@vxgmichel
vxgmichel / qgeventdispatcher.py
Created February 13, 2018 11:38
Qt-gevent dispatcher
import sys
import gevent.event
from collections import defaultdict, deque
from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5.QtCore import QAbstractEventDispatcher, QTimer, QTimerEvent
class QGeventDispatcher(QAbstractEventDispatcher):
@vxgmichel
vxgmichel / binary_search.py
Last active April 18, 2018 13:24
A binary search based on bisect module
import bisect
import itertools
def binary_search(f, lower=None, upper=None, precision=1):
# Switch lower and upper
if lower is None and upper is not None:
return precision - binary_search(
lambda x: not f(-x), -upper, None, precision)
@vxgmichel
vxgmichel / MIT-license.md
Last active April 19, 2018 08:41
This license applies to all public gists https://gist.github.com/vxgmichel
@vxgmichel
vxgmichel / bareiss.py
Last active March 2, 2021 14:47
Pure python implementation of the Bareiss algorithm
def adjugate(a, m=None):
# Initialize
sign = 1
previous = pivot = 1
# Bareiss formula
def do_pivot(a, b, c, d, e):
x = a * d - b * c
if m is None:
q, r = divmod(x, e)
@vxgmichel
vxgmichel / benchmark.py
Created June 27, 2018 10:01
A benchmark comparing sympy and a pure python implementation of the Bareiss algorithm
import sys
import time
import hashlib
from fractions import Fraction
from contextlib import contextmanager
from sympy import Matrix
# https://gist.github.com/vxgmichel/080e9999a1020711f27cd60b5c2d14de
from bareiss import adjugate