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 / 1_scipy_package_usage.csv
Last active July 17, 2025 21:25
scipy usage survey
module usage_count
scipy.stats 1556
scipy.signal 1506
scipy.linalg 1342
scipy.optimize 1319
scipy.sparse 748
scipy.interpolate 497
scipy.special 441
scipy.ndimage 434
scipy.spatial 417
@jorenham
jorenham / hkt.rs
Created June 22, 2025 19:49
Rust Higher Kinded Types (HKT) practical example
trait Family {
type Member<T>;
}
trait Functor<A> {
type Of: Family<Member<A> = Self>;
fn fmap<B>(self, f: impl FnMut(A) -> B) -> <Self::Of as Family>::Member<B>;
}
//
@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

@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
@jorenham
jorenham / primes.py
Last active May 11, 2025 00:59
Python walrus prime numbers
import itertools, typing
def primes(n: int, /) -> typing.Generator[int]:
yield next(sieve := itertools.count(prime := 2))
for _ in range(n):
yield (prime := next(sieve := filter(prime.__rmod__, sieve)))
@jorenham
jorenham / counting_using_generics.py
Created March 22, 2024 02:04
Emulating integer in Python's type system: addition and subtraction
from __future__ import annotations
from typing import Protocol
class _Int(Protocol):
def __int__(self) -> int: ...
def __pos__(self) -> _Int: ...
def __neg__(self) -> _Int: ...
@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__()
@jorenham
jorenham / pause_django_caching.py
Last active October 22, 2021 15:29
Temporarily disable django caching
import contextlib
from typing import ContextManager
from django.core import cache
from django.core.cache.backends.dummy import DummyCache
@contextlib.contextmanager
def disable_caches(*aliases: str) -> ContextManager[tuple[str, ...]]:
"""Temporarily disable django caching backends. By default, all connected