Skip to content

Instantly share code, notes, and snippets.

@vxgmichel
vxgmichel / autobisect.py
Last active May 2, 2020 10:17
Automatically bisect a given function, without providing upper or lower bounds
"""
Automatically bisect a given function, without providing upper or lower bounds.
The provided function is expected to:
- take an integer as single argument
- be defined for all integers (positive and negative)
- return a truth value
- contain a single transition (either true to false or false to true)
The argument corresponding to the true value of the transition is returned.
@vxgmichel
vxgmichel / timecapsule.py
Last active October 15, 2022 14:55
Theoretical solver of the LCS35 Time Capsule Crypto-Puzzle
#!/usr/bin/env python3
"""
Theoretical solver of the LCS35 Time Capsule Crypto-Puzzle
See: https://people.csail.mit.edu/rivest/lcs35-puzzle-description.txt
Example usage:
% time ./timecapsule.py -t 100000
@vxgmichel
vxgmichel / to_aiter_benchmark.py
Created November 18, 2019 14:04
Benchmark several `to_aiter` implementations
"""
Results:
1.77s call test_trio.py::test_to_aiter[run each next call in a separate thread]
1.29s call test_trio.py::test_to_aiter[run iteration in a single thread (buffer_size=inf)]
1.28s call test_trio.py::test_to_aiter[run iteration in a single thread (buffer_size=0)]
1.28s call test_trio.py::test_to_aiter[run iteration in a single thread (buffer_size=1)]
0.45s call test_trio.py::test_to_aiter[run the iteration and sleep(0) between each item]
0.02s call test_trio.py::test_to_aiter[run iteration in a single thread with batches]
0.01s call test_trio.py::test_to_aiter[run each batch iteration in a separate thread]
0.00s call test_trio.py::test_to_aiter[run the iteration in a thread and return a list]
@vxgmichel
vxgmichel / service_nursery_deadlock.py
Last active November 13, 2019 15:20
Demonstrate a potential deadlock with service nurseries
import trio
import pytest
from contextlib import asynccontextmanager
from service_nursery import run_service_nursery
@asynccontextmanager
async def run_deadlock_service():
class service:
@vxgmichel
vxgmichel / trio_cancel_services.py
Created November 12, 2019 09:45
Simultaneous cancellation of nurseries in trio
"""
This tests shows a trio behavior that we did not anticipated.
When a cancellation is issued in trio, all the nurseries in the scope
are cancelled simultaneously. This can sometimes lead to confusing
errors, especially when those nurseries are hidden in context
managers.
This example shows the case of two services, implemented as an async
context manager running a background task. Then, we assume that the
@vxgmichel
vxgmichel / bad_service_manager.py
Created October 16, 2019 09:06
A bad service manager in trio
import trio
from contextlib import AsyncExitStack, asynccontextmanager
@asynccontextmanager
async def some_service():
class Service():
def __init__(self, nursery):
self.nursery = nursery
@vxgmichel
vxgmichel / recgen.rs
Last active October 12, 2019 16:11
Recursive generators in rust
#![feature(generators, generator_trait)]
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;
// Generator to Iterator
struct GeneratorToIterator<G>(G);
impl<G> Iterator for GeneratorToIterator<G>
@vxgmichel
vxgmichel / _primegen.pyx
Last active March 23, 2021 15:34
Python+cython prime generators
# distutils: language=c++
from cython.operator cimport dereference
from libcpp.unordered_map cimport unordered_map
def primes():
# Yield first two primes
yield 2
yield 3
@vxgmichel
vxgmichel / trio_task_monitoring.py
Last active August 28, 2023 18:22
Trio instrument to detect and log blocking tasks
import time
import inspect
import traceback
import trio
import structlog
logger = structlog.get_logger()
@vxgmichel
vxgmichel / continuedfraction.py
Created May 26, 2019 09:02
Python helpers for continued fractions
"""Helpers for continued fractions"""
import math
import itertools
def continued_fraction(n, d):
while d:
q, r = divmod(n, d)
n, d = d, r