Skip to content

Instantly share code, notes, and snippets.

View jorenham's full-sized avatar

Joren Hammudoglu jorenham

  • Delft, the Netherlands
  • YouTube @JorenH
View GitHub Profile
@jorenham
jorenham / ruff_format.py
Last active May 20, 2025 16:41
ruff format a Python string
import subprocess
from pathlib import Path
# see https://github.com/astral-sh/ruff/pull/18224
from ruff.__main__ import ( # type: ignore[import-untyped] # pyright: ignore[reportMissingTypeStubs]
find_ruff_bin, # noqa: PLC2701
)
def ruff_format(source: str) -> str:

original post: scipy/scipy-stubs#153 (comment)


Annotating a function whose return type depends on an optional positional- or keyword-only parameter in Python

Overloads are roughly similar to pattern matching[^1], but from the perspective of the caller.

Consider this function for example

@RandallPittmanOrSt
RandallPittmanOrSt / nptypes.py
Created January 14, 2025 17:44
ndarray shape and type narrowing
"""nptypes.py - Various useful types and type aliases"""
# FUTURE: Integrate optype
from typing import Any, Literal, Protocol, TypeAlias, TypeVar
import numpy as np
import numpy.typing as npt
from typing_extensions import TypeIs
@jorenham
jorenham / scipy-stubs-vscode.md
Last active January 24, 2025 23:00
Comparison of VSCode with and without scipy-stubs
Without scipy-stubs With scipy-stubs
image image
image image
  • VSCode 1.96.2
  • PyLance 2024.12.100
  • pyright == 1.1.391
  • scipy == 1.15.0
@wolph
wolph / tox_ini_to_toml.py
Last active January 2, 2025 01:59
Convert a tox.ini to tox.toml or pyproject.toml file.
#!/usr/bin/env python3
import argparse
import configparser
import os
import re
import shlex
import sys
from typing import Any
@twolfson
twolfson / README.rst
Last active May 9, 2025 08:08
Evaluation and comparison of various Python templating libraries

gist-python-templating-evaluation

@wolph
wolph / pretty-pyright
Last active June 25, 2023 21:35
Little python script to wrap around pyright to add indenting, reformatting and making it easier to read. It also adds pretty colours
#!/usr/bin/env python -u
import os
import re
import sys
import itertools
import contextlib
import subprocess
sys.stdout.reconfigure(line_buffering=True)
@jorenham
jorenham / asyncio_eventloop_benchmark.py
Last active August 29, 2022 23:38
Directly measures the asyncio event loop iteration speed, and compares it with a sync for-loop
import asyncio
import time
from time import perf_counter_ns
# noinspection PyPep8Naming
class ns_per_it:
__slots__ = 'n', 'res'
def __init__(self, n: int):
@jorenham
jorenham / async_init.py
Created November 4, 2021 16:43
async version of __init__
import asyncio
class AsyncInit:
def __await__(self):
async def _():
await self.__ainit__()
return self
return _().__await__()
@almoore
almoore / README.md
Last active October 19, 2024 05:02
Getting realtime output using Python Subprocess