Skip to content

Instantly share code, notes, and snippets.

View kkew3's full-sized avatar

Kaiwen kkew3

View GitHub Profile
@kkew3
kkew3 / nums2ranges.py
Created January 28, 2023 05:06
Compress a list of integers into a list of ranges. For example, `1 2 3 4 5 7 8 9` becomes `1:6 7:10`. The code can be used both as a library and as an executable.
#!/usr/bin/env python3
def nums2ranges(nums):
"""
>>> list(nums2ranges([]))
[]
>>> list(nums2ranges([0, 1, 2, 3, 4]))
[slice(0, 5, None)]
>>> list(nums2ranges([0, 1, 4, 5, 6]))
@kkew3
kkew3 / bisectimgresizer.py
Created December 29, 2022 10:57
Compress image to just below the given size upper bound with `imagemagick` using bisection algorithm.
#!/usr/bin/env python3
import os
import re
import sys
import shlex
import shutil
import argparse
import subprocess
import logging
@kkew3
kkew3 / appselector.sh
Last active December 15, 2022 02:40
Get the bundle identifier of a macOS app. Can be used to set `ascii_mode` of [`Squirrel`](https://rime.im) for apps.
#!/bin/bash
set -e
set -o pipefail
fd -g -td \*.app /System/Library/CoreServices /System/Applications /Applications . \
| fzf --multi --exact --query="$1"
@kkew3
kkew3 / mk_zip_patch.py
Created May 15, 2022 08:55
Make a `patch.zip` file from `old.zip` file to `new.zip` file, including all changed and newly added files. Also generate a patch shell script `patch.sh` that removes all files unique to `old.zip`.
import os
import argparse
import collections
import tempfile
import zipfile
import filecmp
import shutil
import shlex
ZipCmpResult = collections.namedtuple('ZipCmpResult',
@kkew3
kkew3 / pyimports.py
Last active May 14, 2022 22:02
Find all Python imports in source file(s) and/or ipython notebooks, and optionally exclude installed modules, effectively creating a requirements.txt candidate. Python 2.7/3.x compatible.
from __future__ import print_function
import argparse
import os
import ast
import sys
import pkgutil
import itertools
import logging
import json
@kkew3
kkew3 / cjkjust.py
Last active February 9, 2022 09:58
Python `str.ljust` and `str.rjust` for CJK characters mixed with ASCII characters string.
__all__ = [
'cjkljust',
'cjkrjust',
'cjkcenter',
]
import unicodedata
#def cjklen(string):
# return sum(2 if unicodedata.east_asian_width(char) in 'FW' else 1
@kkew3
kkew3 / fmtstrings_like_ls.py
Last active February 5, 2022 10:44
Pretty print list of strings like BSD `ls`
import math
import itertools
import shutil
# https://gist.github.com/kkew3/8bb9aa225a6c82ae5e1a0fa609c9a65a
import cjkjust
def calc_layout(n_strings, total_width, column_width, width_between_cols):
# expected_ncols * column_width +
@kkew3
kkew3 / rbtree.cpp
Created December 6, 2021 03:43
The book "Introduction to Algorithms Third Edition" presents red-black tree operations with nodes including parent pointer `.p`. This gist otherwise gives implementation that does not require the parent pointer in C++.
#include "rbtree.h"
rbnode *left_rotate(rbnode *x)
{
// assert(x && x->right);
rbnode *y = x->right;
x->right = y->left;
y->left = x;
return y;
}
@kkew3
kkew3 / checkvideo.py
Last active December 15, 2021 02:59
Check whether a video file has been corrupted
# original command adapted from https://superuser.com/a/100290
import subprocess
filename = ...
try:
_ = subprocess.check_call(['ffmpeg', '-v', 'error', '-i',
filename, '-f', 'null', '-'])
except subprocess.CalledProcessError:
# the subprocess call returns non-zero, which means filename
#!/usr/bin/env python3
import os
import sys
import argparse
import subprocess
import ast
HOMEDIR = os.path.normpath(os.environ['HOME'])
MUSICDIR = os.path.join(HOMEDIR, 'Music')
COVERARTDIR = os.path.join(MUSICDIR, '.cover-arts')