Skip to content

Instantly share code, notes, and snippets.

View kkew3's full-sized avatar

Kaiwen kkew3

View GitHub Profile
@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')
@kkew3
kkew3 / advindexparser.py
Last active March 1, 2024 18:11
Parse numpy style advanced array indexing notation from string.
import re
SLICE_TEMPLATES = [
('s', r'(?P<i>[+-]?\d+)'),
('sp', r'\((?P<i>[+-]?\d+)\)'),
('a', r'::?'),
('ri-', r'(?P<i>[+-]?\d+)::?'),
('ri-k', r'(?P<i>[+-]?\d+)::(?P<k>[+-]?\d+)'),
('r-j', r':(?P<j>[+-]?\d+):?'),
('r-jk', r':(?P<j>[+-]?\d+):(?P<k>[+-]?\d+)'),
@kkew3
kkew3 / mount-ebs.sh
Created July 27, 2019 12:56
[mount EBS volume to AWS EC2]
#!/bin/bash
# this command list attached volumes; make sure the volume to mount
# is in the list
lsblk
# see if the volume contains file system (skipped if for sure).
# the name of the volume is denoted here as `/dev/xvdf', which
# should be listed in the output of `lsblk'.
# If the output is `/dev/xvdf: data', then it does not have