Skip to content

Instantly share code, notes, and snippets.

@mahdilamb
mahdilamb / opensearch_age_decay.py
Last active February 6, 2026 20:27
Numpy implementation of Opensearch Age decay
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "matplotlib>=3.10.8",
# "numpy>=2.4.2",
# ]
#
# ///
@mahdilamb
mahdilamb / export_env.sh
Created January 15, 2026 20:13
Export specific values
#!/usr/bin/env bash
ALLOWED_VALUES=("$@")
while IFS='=' read -r key value; do
[[ -z "$key" || "$key" =~ ^# ]] && { echo "$key"; continue; }
if [ "${#ALLOWED_VALUES[@]}" -eq 0 ]; then
echo "$key=$value"
continue
@mahdilamb
mahdilamb / Dockerfile
Last active January 13, 2026 23:16
Dockerfile for routing https traffic to http
FROM alpine:latest AS cert-init
RUN apk add openssl
WORKDIR /root/ssl
RUN openssl req -x509 -nodes -newkey rsa:2048 \
-keyout key.pem \
-out cert.pem \
-days 365 \
-sha256 \
-subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
"""Utility script to export the OpenAPI spec from a FastAPI service.
Usage:
python3 fastapi2openapi path.to.module:app -C root/of/package --file openapi.yaml
Where the unnamed argument is the location of the app (the module:app_variable). -C is the working directory to import from. And --file is the file to output.
Note that the output file will be relative to the current working directory and not the location in -C.
"""
@mahdilamb
mahdilamb / compose.yaml
Created September 25, 2024 14:40
compose with mkcert certificates
# Docker compose file for testing the build process locally
services:
frontend:
container_name: frontend
build:
context: .
args:
- NEXT_PUBLIC_LOCAL=http://localhost:8080
ports:
- "3000:3000"
@mahdilamb
mahdilamb / gcs-pubsub-fastapi.py
Created August 18, 2024 12:53
Script for creating a FastAPI endpoint for google cloud storage pub/sub
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pydantic",
# "fastapi",
# "uvicorn[standard]",
# ]
# ///
import datetime
"""
Live utility for converting `.model_validate({...})` calls into
model constructor calls with snake_case keyword arguments.
The module watches itself for changes, reloads on save, transforms
the code in `SAMPLE` and copies it to the clipboard.
"""
import importlib.util
import os
def _show_commands():
"""Print the help for each of the functions."""
import inspect
import tokenize
import scripts as module
def get_docs(fns: dict[str, str]) -> dict[str, str]:
result = {}
@mahdilamb
mahdilamb / hibernate-after.ps1
Created June 10, 2024 17:04
Hibernate the computer after a number of seconds. Format: `mm:ss`
param($DURATION)
$_POS = $DURATION.IndexOf(":")
$MINS = $DURATION.SubString(0, $_POS)
$SECS = $DURATION.SubString($_POS + 1, $DURATION.Length - [Int64]$_POS - 1)
$TOTAL_SECS = (([Int64]$MINS) * 60) + $SECS
$REMAINING_SECS = $TOTAL_SECS
while ($REMAINING_SECS -gt 0) {
Write-Host -NoNewLine "`rSleeping after $REMAINING_SECS seconds"
Start-Sleep -s 1
$REMAINING_SECS--
@mahdilamb
mahdilamb / kaggle-cp310-to-cp311.sh
Created May 17, 2024 08:37
Migrate a kaggle notebook environment to Python 3.11
PYTHON3_USER_SITE=$(python3 -c 'import site;print(site.USER_SITE)')
[ $(python3 --version | grep '3.11') ] || (rm -rf /opt/conda & sudo add-apt-repository ppa:deadsnakes/ppa &&
sudo apt update &&
sudo apt install -yqq python3.11 python3.11-distutils &&
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 &&
sudo update-alternatives --install /usr/bin/python3 python3 $(which python3.11) 1 &&
sudo update-alternatives --config python3 &&
python3 -m pip install -q packaging requests kaggle &&
# python3 -m pip config set global.extra-index-url $(python3 -c 'import subprocess, requests, re, packaging.version;cuda_version = int(next(result for result in re.findall( r"(?<=release )(\d+)|(^-1)",subprocess.check_output(r"nvcc -V", shell=True).decode(),flags=re.MULTILINE,)[0] if result));print("https://download.pytorch.org/whl/"+(sorted([version for version in re.findall(r"^\<a href=\"(cpu|cu\d+)\/torch-(\d+\.\d+\.\d+)",requests.get("https://download.pytorch.org/whl/torch_stable.html"