Skip to content

Instantly share code, notes, and snippets.

@yeiichi
yeiichi / parent_url_extractor.py
Created July 4, 2025 01:43
Extract the parent directory path from the given file path
#!/usr/bin/env python3
from urllib.parse import urlparse, urlunparse, ParseResult
# Constants
ROOT_PATH = '/'
def extract_parent_path(path: str) -> str:
"""
Extracts the parent directory path from the given file path.
@yeiichi
yeiichi / week_number_solver.py
Last active July 2, 2025 00:49
Generate a list of ISO week date ranges (Monday to Sunday) for a given year
#!/usr/bin/env python3
import argparse
from collections import namedtuple
from datetime import date
Week = namedtuple("Week", ["week_number", "start_date", "end_date"])
def list_iso_week_dates(year):
"""
@yeiichi
yeiichi / wdi_country_region_code.csv
Created June 30, 2025 00:46
World Development Indicators contry or region code
We can make this file beautiful and searchable if this error is corrected: It looks like row 10 should actually have 9 columns, instead of 2 in line 9.
country_region_code,short_name_wdi,short_name_mofa,region_wdi,region_mofa,remarks,wdi_asof,mofa_asof,mofa_url_ja
ABW,Aruba,not_defined,Latin America & Caribbean,not_defined,blank,2022-08-01,not_defined,not_defined
AFE,Africa Eastern and Southern,not_defined,not_defined,not_defined,blank,2022-08-01,not_defined,not_defined
AFG,Afghanistan,アフガニスタン,South Asia,中東,blank,2022-08-01,2022-08-06,https://www.mofa.go.jp/mofaj/area/afghanistan/index.html
AFW,Africa Western and Central,not_defined,not_defined,not_defined,blank,2022-08-01,not_defined,not_defined
AGO,Angola,アンゴラ,Sub-Saharan Africa,アフリカ,blank,2022-08-01,2022-08-06,https://www.mofa.go.jp/mofaj/area/angola/index.html
ALB,Albania,アルバニア,Europe & Central Asia,欧州,blank,2022-08-01,2022-08-06,https://www.mofa.go.jp/mofaj/area/albania/index.html
AND,Andorra,アンドラ,Europe & Central Asia,欧州,blank,2022-08-01,2022-08-06,https://www.mofa.go.jp/mofaj/area/andorra/index.html
ARB,Arab World,not_defined,not_defined,not_defined,blank,2022-08-01,not_defined,not_defined
ARE,United
@yeiichi
yeiichi / max_iso_week.py
Last active June 24, 2025 04:21
Calculate the maximum ISO week number for a given year
#!/usr/bin/env python3
from datetime import date
def get_max_iso_week(year: int) -> int:
"""
Calculates the maximum ISO week number for a given year.
ISO weeks run from Monday to Sunday. Week 1 is the week
with the year's first Thursday. The week containing December 28
@yeiichi
yeiichi / number_chunks_extractor.py
Last active June 20, 2025 23:18
Extract numeric chunks from a string
#!/usr/bin/env python3
import re
import unicodedata
from typing import List
# Constants
KANJI_DIGITS = '〇零一壱二弐三参四五伍六七八九'
ARABIC_DIGITS = '001122334556789'
KANJI_TO_ARABIC_MAPPING = str.maketrans(KANJI_DIGITS, ARABIC_DIGITS)
UNICODE_NORMALIZATION_FORM = 'NFKC'
@yeiichi
yeiichi / django-permission-utils.sh
Last active June 16, 2025 09:39
Setup file and directory ownership and permissions for a Django project
#!/usr/bin/env sh
set -e
# Constants
DEFAULT_GROUP="www-data"
CWD="$(pwd)" # Get current working directory immediately
VENV_DIR="${CWD}/.venv" # Define VENV_DIR relative to CWD
BACKUP_FILE="backup_$(basename "${CWD}")_$(date +%Y%m%d%H%M%S).tar.gz" # Backup file name
@yeiichi
yeiichi / marked_dir_finder.sh
Last active June 18, 2025 02:48
Traverse the directory tree upwards to the marked directory
#!/usr/bin/env sh
# Traverse the directory tree upwards to the marked directory.
MARKER_DIR=".git" # Arbitrary directory like '.git', '.env', etc.
display_help() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Options:
--help Display this help message and exit.
@yeiichi
yeiichi / gitignore.txt
Created June 10, 2025 21:58
gitignore item list
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
@yeiichi
yeiichi / disable_ms_mau_steps.txt
Created June 7, 2025 07:35
Disable Microsoft AutoUpdate (MAU) + OneDrive auto-update
# === Disable Microsoft AutoUpdate (MAU) + OneDrive auto-update ===
# === macOS cheat sheet ===
# My profile: Mac only / Office 365 (Word, Excel, PPT, OneDrive) / Teams in browser
# STEP 1: Add blocklist to /etc/hosts
sudo vi /etc/hosts
# Add this block:
127.0.0.1 officecdn.microsoft.com
127.0.0.1 officecdn.microsoft.com.edgesuite.net
@yeiichi
yeiichi / csv_squarer.py
Created June 2, 2025 02:08
Make a CSV file to be square by removing irregular rows
#!/usr/bin/env python3
import csv
import logging
from collections import Counter
from pathlib import Path
logging.basicConfig(level=logging.INFO)
class CSVSquarer: