Skip to content

Instantly share code, notes, and snippets.

@xyb
xyb / shell.py
Created September 30, 2024 04:29 — forked from ddahan/shell.py
FastAPI context-aware shell with model discovery and other auto-imports
# Requires Python 3.12+, IPython, rich
import datetime
import hashlib
import importlib
import inspect
import json
import logging
import os
import pkgutil
@xyb
xyb / excel.py
Created November 2, 2023 03:41
generate reproducable excel file with openpyxl
# requires:
# pip install openpyxl repro-zipfile
from pathlib import Path
import openpyxl
from repro_zipfile import ReproducibleZipFile
def save_reproducible_excel(path: Path, workbook: openpyxl.Workbook) -> None:
"""save a reproducible/deterministic excel file
@xyb
xyb / middleware.py
Last active August 23, 2023 05:32 — forked from tclancy/middleware.py
Django middleware for Pympler (compatible with Django 2.0+)
# Derived from Piotr Maliński's example with a few modifications to use logging and :
# http://www.rkblog.rk.edu.pl/w/p/profiling-django-object-size-and-memory-usage-pympler/
import logging
import os
from django.conf import settings
from pympler import muppy
from pympler.asizeof import asizeof
from pympler.muppy import summary
@xyb
xyb / osdb_hash.py
Last active August 20, 2023 12:09
compute osdb (opensubtitles.org) hash code with the file size value, allowing to not download the entire file
import struct
import os
BLOCK_SIZE = 65536
MIN_SIZE = BLOCK_SIZE * 2
# based on https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes#Python
def osdb_hash(filename, filesize=0):
try:
@xyb
xyb / frontend-deploy-with-k8s.md
Created April 6, 2023 05:59
Deploying a Frontend Application with Dynamic Environment Variables Using Docker, Kubernetes, and Nginx

Deploying a Frontend Application with Dynamic Environment Variables Using Docker, Kubernetes, and Nginx

This guide demonstrates how to deploy a frontend application using Docker and Kubernetes (k8s) with dynamic environment variables that can change across different namespaces. We will use Nginx as a web server to serve the frontend static files.

1. Create a Dockerfile

Create a Dockerfile for your frontend project:

# Use a Node.js base image
FROM node:14 AS build
@xyb
xyb / excel_tools.py
Created April 4, 2023 08:39
some functions to help read or generate excel files
import csv
import io
import logging
import openpyxl
logger = logging.getLogger("excel")
def excel_to_csv(path):
@xyb
xyb / convert-excel-to-csv.py
Created March 23, 2023 08:09
convert excel to csv
import openpyxl
import csv
import sys
input = sys.argv[1]
output = sys.argv[2]
print(f'{input} ==> {output}')
wb = load_workbook(input)
sh = wb.active
@xyb
xyb / unmerge-excel-cell-and-export-as-csv.py
Last active March 23, 2023 08:07
unmerge excel cell and export as csv
#!/usr/bin/env python3
# Usage: $0 input.xlsx output.csv
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
from openpyxl.utils.cell import range_boundaries
import sys
import csv
@xyb
xyb / audio-volumes.py
Last active February 17, 2023 03:23
get audio volumes samples, assited by ChatGPT
from pydub import AudioSegment
import numpy as np
def get_volumes(audio_path, format, chunks_number=100):
audio = AudioSegment.from_file(audio_path, format=format)
segment_length = len(audio) // chunks_number
volumes = []
for i in range(chunks_number):
@xyb
xyb / 00-install-facetime-camera.sh
Last active May 5, 2024 20:47 — forked from ukn/99-install-facetime-camera.sh
Install the kernal module required for the facetimehd camera to work on Linux
#!/bin/bash
set -e
export CONFIG_MODULE_SIG=n
export CONFIG_MODULE_SIG_ALL=n
# For current kernel
export KERNELRELEASE=$(cat /proc/version | awk '{print $3}')
temp_dir=$(mktemp -d)
echo "Installing FacetimeHD camera for $KERNELRELEASE"