Skip to content

Instantly share code, notes, and snippets.


Roger Qiu CMCDragonkai

Block or report user

Report or block CMCDragonkai

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
CMCDragonkai / file-s3-etag
Last active Apr 15, 2020
Calculate S3 ETAG including Multipart Uploads #python #aws
View file-s3-etag
#!/usr/bin/env python3
import argparse
import hashlib
# see
# for default multipart_threshold and multipart_chunksize
def md5sum(
file_like, multipart_threshold=8 * 1024 * 1024, multipart_chunksize=8 * 1024 * 1024
CMCDragonkai /
Last active Apr 11, 2020
Self-Referential/Recursive Dictionary in Strict Languages #python #javascript

Self-Referential/Recursive Dictionary in Strict Languages

The problem with this in strict languages is that we keep needing lazy annotations.

And the other problem is that this won't be very fast in strict languages.

In strict languages there is probably no caching of the function calls.

What this means is comparing a recursive dictionary to a static dictionary, the recursive dictionary is going to be slower depending on how many function calls there are. In fact the slow down may be linear to the number of self or that function calls.

CMCDragonkai /
Last active Apr 9, 2020
Acquire the earliest common descendant for 2 Git commit revision hashes #git
#!/usr/bin/env bash
branch="$(git rev-parse --abbrev-ref HEAD)"
if git merge-base --is-ancestor "$commit_1" "$commit_2"; then
echo "$commit_2"
exit 0
CMCDragonkai /
Created Mar 25, 2020
Singleton List Syntax #python #haskell

Singleton List Syntax

Sometimes you want to have a singleton list or an empty list.

In Python:

[1] * (True) # [1]
[1] * (False) # []
CMCDragonkai /
Last active Mar 13, 2020
Load Images and Numpy Arrays from Filesystem URLs and From Standard Input #python
import io
import sys
import fs
import contextlib
import urllib.parse
import numpy as np
from PIL import Image
from typing import Tuple, Iterator, IO, Optional, List
CMCDragonkai /
Created Jan 20, 2020
Building the NixOS netboot release.nix (from the nixpkgs source) #nix
#!/usr/bin/env sh
revCount="$(git rev-list --count HEAD)"bootstrap
shortRev="$(git rev-parse --short HEAD)"
nix-build nixos/release.nix \
--attr netboot.x86_64-linux \
--arg nixpkgs "{ outPath = ./.; revCount = $revCount; shortRev = \"$shortRev\"; }" \
-o netboot
CMCDragonkai / docker_containers_with_cacert_and_tzdata.nix
Created Jan 8, 2020
Docker Containers with CA certificates and timezone database built using Nix #cli #nix #docker
View docker_containers_with_cacert_and_tzdata.nix
# if you have networked applications that use the TLS or timezones
# you need to have these dependencies in the container
# note that these don't get put in the default.nix derivation
# because the package will use the OS provided cacert and tzdata
# but inside the container, they will need these 2 bundled explicitly
# or mounted...
# both cacert and tzdata has setup hooks
# this means when the container is built
# these pieces of data gets added into the container root filesystem
# at their standard locations
CMCDragonkai /
Last active Nov 20, 2019
Virtualizing Linux USB Host Controller Drivers for QEMU #linux #qemu

Virtualizing Linux USB Host Controller Drivers for QEMU

USB is a device-host protocol. So the host, which is likely your operating system requires a host controller interface (which is a piece of hardware).

The host controllers require drivers to run.

There are 3 kinds of USB host controller drivers:

  • ohci - Open Host Controller Interface - USB 1
CMCDragonkai /
Created Nov 11, 2019
QEMU-KVM boot a new x86-64 Virtual Machine from kernel and initrd #qemu
#!/usr/bin/env sh
# assume you have bzImage, initrd, cmdline, share, bootdisk, maindisk
# the below will boot a QEMU VM with 2 disks attached (as raw), with host port 55555 mapped to 22
# and also pass the share directory into the VM
qemu-system-x86_64 \
-kernel ./bzImage \
-initrd ./initrd \
-smp $(nproc) \
You can’t perform that action at this time.