This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import asyncio | |
from contextlib import asynccontextmanager | |
from typing import AsyncIterable, AsyncIterator, TypeVar | |
from anyio import create_memory_object_stream, create_task_group | |
from anyio.abc import ObjectReceiveStream, TaskStatus | |
from anyio.streams.memory import MemoryObjectSendStream | |
T = TypeVar("T") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import time | |
import random | |
import shutil | |
import argparse | |
from urllib.request import Request, urlopen | |
from imageio import imread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Serve a ptpython console using both telnet and ssh | |
""" | |
import pathlib | |
import asyncio | |
import asyncssh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 =========== |
NewerOlder