Skip to content

Instantly share code, notes, and snippets.

@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)
@yeiichi
yeiichi / column_contract_typA.csv
Created January 7, 2026 00:58
Semantic contract for long-format datasets (Type A)
Column Type Meaning Rules
entity string Thing being measured Stable label, not human prose
variable string What is measured Atomic, no units
value numeric Measured value Float/decimal only
unit string Measurement unit %, g/100g, mg/L, etc.
basis string Unit basis w/w, w/v, dry, as_is
method string Measurement method Optional
source string Source name File/site/report
source_id string Stable source key URL, DOI, hash
observed_at string/date When observed ISO-8601
@yeiichi
yeiichi / mdtable2csv.sh
Created January 7, 2026 00:49
Convert Markdown tables to CSV
#!/usr/bin/env bash
set -euo pipefail
# mdtable2csv: Convert Markdown tables to CSV.
# Handles alignment rows, trims cells, escapes CSV safely, and respects escaped pipes (\|).
mdtable2csv() {
local in out
if [[ $# -lt 1 || $# -gt 2 ]]; then