Skip to content

Instantly share code, notes, and snippets.

View kkew3's full-sized avatar

Kaiwen kkew3

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / import_imgs.pl
Last active February 7, 2023 12:28
Use (a little more intelligent) bisection algorithm to resize images under $from_dir to appropriate small size, and move them to $to_dir. The whole process is performed interactively. This script is designed for macOS and requires imagemagick to run.
use warnings;
use strict;
use File::Basename qw(fileparse);
use File::Copy qw(cp mv);
######################################################################
# Configuration #
######################################################################
@kkew3
kkew3 / hull_plot.py
Last active February 5, 2023 21:27 — forked from nicoguaro/hull_plot.py
Plot the convex hull around a set of points as a shaded polygon.
# -*- coding: utf-8 -*-
"""
Plot the convex hull around a set of points as a
shaded polygon.
@author: Nicolas Guarin Zapata and Kaiwen
@date: February 6, 2023
"""
import numpy as np
from scipy.spatial import ConvexHull
@kkew3
kkew3 / sum_filesize.sh
Last active February 17, 2023 17:27 — forked from fsteffenhagen/sum_filesize.sh
sum human readable file sizes from `du` with `numfmt` and `perl`
# Require: [`fd`](https://github.com/sharkdp/fd.git)
PATTERN='XXX' # Assume this would be a directory
fd -0 -td "$PATTERN" \
| xargs -0 du -sh \
| numfmt --from=auto --to=none \
| perl -ane 'BEGIN { my $sum = 0; } $sum += $F[0]; print; END { print $sum, " totoal\n"; }' \
| numfmt --from=none --to=si