Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
vxgmichel / pancake.hs
Created July 10, 2018 16:03
Solve the 'Infinite House of Pancakes' problem
-- Solve the 'Infinite House of Pancakes' problem.
-- https://code.google.com/codejam/contest/6224486/dashboard#s=p1
import Text.Printf
-- Generic solver code
main :: IO ()
main = interact
$ unlines . cases . map (format . solve) . parse . tail . lines
@vxgmichel
vxgmichel / long_sleep.py
Last active October 19, 2020 15:38
Asyncio long sleep (over 1 day)
import asyncio
async def long_sleep(arg):
hours = 60*60
async def bg():
while True:
await asyncio.sleep(12 * hours)
@vxgmichel
vxgmichel / parallelism.py
Created July 17, 2018 12:07
Test parallelism in python
"""Test parallelism in python.
$ \time -f "%e s - %P" python3 par.py 1 > /dev/null
6.74 s - 100%
$ \time -f "%e s - %P" python3 par.py 4 > /dev/null
1.87 s - 385%
$ bc -l <<< "6.74/1.87"
3.60...
"""
@vxgmichel
vxgmichel / compat.py
Created July 26, 2018 21:14
Asyncio/Curio/Trio compatibility module
import asyncio
try:
import curio
except ImportError:
curio = None
try:
import trio
except ImportError:
@vxgmichel
vxgmichel / foo.py
Created August 10, 2018 12:26
Answering on stack overflow - see the docstring
"""
A corrected version of the program in following stackoverflow question:
https://stackoverflow.com/questions/51775413/python-asyncio-run-coroutine-threadsafe-never-running-coroutine
"""
import asyncio
from threading import Thread
from contextlib import contextmanager
@vxgmichel
vxgmichel / merge.py
Last active November 15, 2018 13:42
Merging async generators with trio
import trio
import random
async def merge(agens):
async def produce(agen, channel):
async with channel:
async for item in agen:
await channel.send(item)
@vxgmichel
vxgmichel / merge.py
Last active November 16, 2018 10:14
Safely merging async generators with trio
import random
from contextlib import asynccontextmanager
import trio
@asynccontextmanager
async def aitercontext(resource):
aiterator = resource.__aiter__()
@vxgmichel
vxgmichel / trio-asyncgen-writeup.md
Last active November 22, 2018 13:24
Using trio nurseries inside async generators

Using trio nurseries inside async generators

From what I've read in this comment (issue #264), it seems like the idea of:

  • allowing yield inside a trio nursery
  • AND allowing it to cancel the current task regardless of its current position in the code

is out of the picture. The main argument against that is presented in this comment (issue 638):

@vxgmichel
vxgmichel / groupby.py
Last active October 13, 2021 15:04
Asynchronous implementation of itertools.groupby for aiostream
import asyncio
from aiostream.aiter_utils import anext
from aiostream import stream, operator, streamcontext, pipe
import pytest
@operator(pipable=True)
async def groupby(source, key=None):
@vxgmichel
vxgmichel / bandit.sh
Created December 15, 2018 19:05
Solving Bandit from OverTheWire
#!/usr/bin/env bash
SERVER=bandit.labs.overthewire.org
PORT=2220
BASEUSER="bandit"
PASSWORD="bandit0"
COMMANDS[0]='cat readme'
COMMANDS[1]='cat ./-'
COMMANDS[2]='cat "spaces in this filename"'