Skip to content

Instantly share code, notes, and snippets.

@yeiichi
yeiichi / generate_numbers_csv.py
Created May 5, 2026 01:31
Generate a CSV-ready table of number attributes.
from __future__ import annotations
from math import isqrt
# An example for a typical downstream usage is available at the bottom of the page.
def is_prime(n: int) -> bool:
if n < 2:
return False
if n == 2:
@yeiichi
yeiichi / webarchive_to_html.py
Created April 17, 2026 07:56
Extract HTML and resources from an Apple .webarchive file using Python's plistlib
#!/usr/bin/env python3
"""
webarchive_to_html.py
Extract HTML and resources from an Apple .webarchive file using Python's plistlib,
then rewrite common resource links for better offline viewing.
Usage:
python webarchive_to_html.py input.webarchive
python webarchive_to_html.py input.webarchive -o output_dir
@yeiichi
yeiichi / whitespace_removers.py
Created March 30, 2026 09:05
Remove whitespace from text using different strategies
import re
class WhitespaceRemover:
"""Remove whitespace from text using different strategies.
All methods remove *Unicode whitespace* (not just ASCII spaces).
Choose a method based on readability, performance, and workload size.
"""
@yeiichi
yeiichi / zsh-venv-prompt.zsh
Created March 26, 2026 12:07
Configures a dynamic Zsh prompt that displays the current Python virtual environment name without breaking standard formatting
# Set up the prompt
# 1. This MUST be here for the $(...) or ${...} to work
setopt PROMPT_SUBST
# 2. Tell the venv script not to mess with the prompt
export VIRTUAL_ENV_DISABLE_PROMPT=1
# 3. The prompt string (no spaces inside the parentheses)
PROMPT='${VIRTUAL_ENV:+(${VIRTUAL_ENV:t}) }%m:%1~ %# '
@yeiichi
yeiichi / datetime_util.py
Created March 20, 2026 01:34
Utility helpers for working with timezone-aware datetimes
"""
datetime_util.py
Utility helpers for working with timezone-aware datetimes.
This module provides:
- Predefined, commonly used timezone constants (UTC, JST, ET, etc.)
- Convenience functions for getting the current time in specific regions
- Safe conversion utilities that enforce timezone awareness
- ISO 8601 serialization and parsing helpers
@yeiichi
yeiichi / num_parser.py
Created March 6, 2026 01:22
Parse a numeric string into int or float
def parse_number(x: int | float | str | None):
"""Parse a numeric string into int or float.
Return None for empty or common null markers; leave non-string inputs unchanged.
"""
if not isinstance(x, str):
return x
s = x.strip().replace(",", "")
@yeiichi
yeiichi / apt-repair-clean.mk
Created February 13, 2026 01:06
Repair and clean apt installed packages
# 1. Configures unpacked packages
# 2. Fixes broken dependencies
# 3. Removes unused packages
# 4. Clears out the local repository of retrieved package files
repair-clean: ##
@sudo dpkg --configure -a && \
sudo apt install -f && \
sudo apt autoremove -y && \
sudo apt autoclean
@yeiichi
yeiichi / Makefile_rsync.mk
Last active February 7, 2026 07:06
Selective JSON/JSONL sync with preserved directory structure
# --- Configuration ---
SOURCE_DIR := /path/to/src/
DEST_HOST := host_name_in_ssh_config
DEST_PATH := /path/to/dest
# --- Flags ---
# -a: Archive (preserve permissions/times)
# -v: Verbose (see what's happening)
# -z: Compress (faster over network)
# -R: Relative (ensures directory structure is preserved)/Don't use -> redundant in this context
@yeiichi
yeiichi / Mekefie_mysql.mk
Created January 29, 2026 08:19
FreeBSD-friendly Makefile for mysql
# --- Configuration Constants ---
DB_HOST ?= mysql.example.com
DB_NAME ?= dbname
DB_USER ?= dbuser
BACKUP_DIR ?= ./backups
DATE := $(shell date +%Y-%m-%d_%H-%M-%S)
# Restore settings
# Usage: make restore RESTORE_FILE=./backups/xxx.sql.gz
RESTORE_FILE ?=
@yeiichi
yeiichi / dataframe_stripper.py
Created January 15, 2026 07:17
Strip leading and trailing whitespace from DataFrame string cells, leaving other types unchanged
def strip_df(df):
"""Strip leading and trailing whitespace from DataFrame string cells, leaving other types unchanged."""
return df.map(lambda s: s.strip() if isinstance(s, str) else s)