Skip to content

Instantly share code, notes, and snippets.

@mezhaka
mezhaka / profile_example.py
Created August 28, 2018 06:23
Printing profiling stats example
#!/usr/bin/env python3.4
import cProfile
import pstats
def first():
pass
def second():
@mezhaka
mezhaka / threadpool_queue.py
Last active March 26, 2021 13:54
Show threadpool processes only max_workers tasks at the same time and behaves like a queue
# Show threadpool processes only max_workers tasks at the same time and behaves
# like a queue
import itertools
from concurrent import futures
from io import StringIO
from time import sleep
def print_sleep_print(p, out):
print("(", file=out, end="")
# A snippet to show that pymongo / mongo converts timezone aware datetime objects to UTC, when saving.
# docker run --env MONGO_INITDB_DATABASE=brain --env MONGO_INITDB_ROOT_USERNAME=root --env MONGO_INITDB_ROOT_PASSWORD=secret --publish-all mongo:4.2
#
# lookup which port it was mapped to:
# $ docker container ls
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 8eaf2dd5aedb mongo:4.2 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:55000->27017/tcp brave_moser
import datetime
@mezhaka
mezhaka / async_temp.py
Created July 21, 2023 13:49
Async wrappers for tempfile.NamedTemporaryFile and tempfile.NamedTemporaryFile.
# Motivation: I had to use standard tarfile to create an archive within an async function.
# def pack(f):
# with tarfile.open(output, "w:gz") as tar:
# tar.add(src_dir, arcname="blah")
#
# async with temp_file() as f:
# asyncio.get_running_loop().run_in_executor(None, pack, f)
@contextlib.asynccontextmanager
async def temp_file() -> Iterator[pathlib.Path]: