Skip to content

Instantly share code, notes, and snippets.

@khelwood
khelwood / clip.py
Created October 26, 2025 22:35
This is the clipboard lib that I use on macs
import subprocess
if str is bytes:
def copy(text):
p = subprocess.Popen(['pbcopy', 'w'],
stdin=subprocess.PIPE, close_fds=True)
p.communicate(input=str(text))
def paste():
p = subprocess.Popen(['pbpaste', 'r'],
@khelwood
khelwood / rgb.py
Created October 26, 2025 22:33
Class vaguely representing RGB colours in renpy games
#!/usr/bin/env python3
class RGB(tuple):
LUM_SCALE=(0.35,0.48,0.17)
def __new__(cls, *args):
if not args:
raise TypeError('A string or tuple is required for an RGB')
if len(args) == 1:
arg, = args
else:
@khelwood
khelwood / combined_replace.py
Created October 26, 2025 22:32
Tool for doing lots of text replacements in different ways
#!/usr/bin/env python3
import re
from collections import defaultdict
class PtnReps:
def __init__(self, flags, reps):
parts = []
@khelwood
khelwood / excel_merged_cells.py
Created September 11, 2025 11:29
Read excel file containing merged cells in Python
import openpyxl
def scan_merges(sheet):
merges = {}
for m in sheet.merged_cells.ranges:
c = m.start_cell.coordinate
for pos in m.cells:
merges[pos] = c
return merges
@khelwood
khelwood / fixicon.py
Created August 18, 2025 20:14
Fix the mac icons of a renpy app
#!/usr/bin/env python3
import sys
import os
import subprocess as sp
from tempfile import TemporaryDirectory
RPA = os.path.expanduser('~/bin/rpatool')
TOICNS = os.path.expanduser('~/Code/python/toicns.py')
@khelwood
khelwood / toicns.py
Last active August 19, 2025 17:53
script to create an icns file for a mac app
#!/usr/bin/env python3
import sys
import os
import itertools
from typing import Optional, Any, TypeAlias
from PIL import Image
Size: TypeAlias = tuple[int,int]
MIN_SIZE = (512,512)
@khelwood
khelwood / multisub.py
Created May 23, 2024 14:26
Python multisub
#!/usr/bin/env python3
import re
class MultiSub:
def __init__(self, subs:dict):
self.subs = subs
self.ptn = re.compile('|'.join(map(re.escape, subs)))
def replacement(self, m):
@khelwood
khelwood / lineedit.py
Last active August 27, 2023 11:34
tool for editing text as a sequence of lines
"""
Tool for editing a large block of text split into lines.
This can make small changes much faster.
Also provides lots of methods for searching and mutating
the text.
"""
import re as _re
from typing import NamedTuple, Match
@khelwood
khelwood / dumpschema.py
Last active September 20, 2023 15:08
Python script to dump a mysql schema
#!/usr/bin/env python3
"""Dumps the table definitions (and optionally view definitions)
of a specified schema using command-line mysql commands.
If you don't specify --views or --tables, table definitions will be
included."""
import subprocess
import sys
@khelwood
khelwood / decrypt_dbvis.py
Last active August 25, 2022 18:07 — forked from gerry/decrypt_dbvis.py
A quick hack to extract and decrypt credentials from DbVisualizer config files. Updated to Python 3.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# decrypt_dbvis.py ~ gerry@twitter.com
# DbVisualizer uses PBEWithMD5AndDES with a static key to store passwords.
# This is a quick hack to extract and decrypt credentials from DbVisualizer config files.
# Tested against DbVisualizer Free 9.0.9 and 9.1.6
#
# install lxml and pycryptodome
#