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.
| 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) |
| 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 |
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.
| # Common imports | |
| from time import sleep | |
| # Synchronous imports | |
| from concurrent.futures import ThreadPoolExecutor | |
| # Asynchronous imports | |
| # from gevent.threadpool import ThreadPoolExecutor | |
| 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) |
| 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): |
| 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) |
All public gists https://gist.github.com/vxgmichel
Copyright 2018, Vincent Michel
MIT License, http://www.opensource.org/licenses/mit-license.php
| 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) |
| 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 |