Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
vxgmichel / amap.py
Created April 19, 2024 11:13
Async map with task limit using anyio
import math
import asyncio
from contextlib import asynccontextmanager
from typing import AsyncIterable, AsyncIterator, Awaitable, Callable
from anyio import create_memory_object_stream, create_task_group, abc
from anyio.streams.memory import MemoryObjectReceiveStream
@asynccontextmanager
async def amap[
@vxgmichel
vxgmichel / Cargo.toml
Last active October 11, 2023 11:56
Comparing rust and python on a specific set intersection problem
[package]
name = "intersection"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
ahash = ["dep:ahash"]
@vxgmichel
vxgmichel / convert_cp437.py
Last active October 22, 2023 22:46
Video conversion for Turing Complete 80x48 display.
#!/usr/bin/env python
"""
Convert an mp4 video file to uncompressed 80x48 video frames.
The output data is meant to be used as direct memory for the 80x24 Turing complete
console. In particular, pixels are grouped vertically so 2 pixels can fit into a
single `▀` (`\xdf`) character.
For instance, the following frame:
@vxgmichel
vxgmichel / stackless.py
Last active October 18, 2021 13:34
Write recursive functions as coroutines to make them stackless
"""
Write recursive functions as coroutines to make them stackless.
Example:
>>> @runable # Allow the function to run with a simple sync call
... @functools.cache # Cache the results
... @stackless # Make the coroutine stackless
... async def fib(n, m=10**9):
... if n <= 1:
@vxgmichel
vxgmichel / pokesprite.py
Created February 17, 2021 13:56
Fetch and display pokemon sprites directly in the terminal
#!/usr/bin/env python3
import json
import time
import random
import shutil
import argparse
from urllib.request import Request, urlopen
from imageio import imread
@vxgmichel
vxgmichel / remote-term.py
Created May 21, 2020 16:20
Serve a ptpython console using both telnet and ssh
#!/usr/bin/env python
"""
Serve a ptpython console using both telnet and ssh
"""
import pathlib
import asyncio
import asyncssh
@vxgmichel
vxgmichel / fenwick.py
Created May 18, 2020 21:14
Fenwick tree - performing both fast sum and fast update on a list of number
"""
Fenwick tree - performing both fast sum and fast update on a list of number
This implementation is mostly compatible with regular list operations.
Time complexity table:
| Operation | List | Fenwick tree |
|-------------|------|--------------|
| Instantiate | O(n) | O(n) |
@vxgmichel
vxgmichel / sumrange.py
Last active May 17, 2020 16:16
Compute the sum of a range without iterating it
"""
Helper to compute the sum of a range without iterating it.
Equivalent to sum(range(start, stop, step)).
"""
def sumrange(start, stop=None, step=1):
# Handle args
if stop is None:
@vxgmichel
vxgmichel / trio_cancel_and_raise.py
Created April 15, 2020 10:18
Run a trio example mixing cancellation and exceptions
"""
Run a trio example mixing cancellation and exceptions.
Shows that the behavior can be hard to predict since it depends on factors like:
- whether a nursery is involved
- whether a checkpoint is performed after the raise
Produces the following output:
=========== cancel_and_raise ===========
@vxgmichel
vxgmichel / stackless.py
Last active April 6, 2020 01:32
Stackless recursion using generators
"""
A decorator to turn a generator into a cached, stackless recursive function.
The yield keyword is used to send the arguments to pass to the recursive
function and retrieve the return value, i.e:
@stackless
def fibonacci(n):
if n < 2:
return n