Skip to content

Instantly share code, notes, and snippets.


Roger Qiu CMCDragonkai

View GitHub Profile
CMCDragonkai /
Last active Apr 27, 2021
Singleton Process with PID Lock File #python
import os
import fcntl
import contextlib
import time
def lockpidfile(filepath):
with os.fdopen(, os.O_RDWR | os.O_CREAT, mode=0o666),
CMCDragonkai / Duplex Async
Last active Apr 5, 2021
Duplex Async Generator #javascript #typescript
View Duplex Async

Duplex Async Generator

This code demonstrates the how to combine a reading generator and a writing generator together into a duplex generator.

The duplex generator will simultaneously read and write at the same time from both the readable and writable generator.

Throwing an exception throws it to the writable generator, but we continue on (but make sure to skip 1 iteration by using the errored flag).

We use the null value to indicate the ending of the generator, it ends both reading and writing sides.

CMCDragonkai /
Last active Mar 30, 2021
Understanding systemd Boot Execution Graph #systemd
CMCDragonkai /
Created Mar 16, 2021
NPM Clean - Needed if SHA1 hashes are appearing #npm

NPM Clean - Needed if SHA1 hashes are appearing

npm cache clear --force
rm -r node_modules/
rm package-lock.json
npm install
CMCDragonkai /
Created Mar 13, 2021
Edit the Boot Command Line with systemd #linux #nixos #systemd

Edit the Boot Command Line with systemd

If you hit a boot error where there is a start job that cannot be terminated, you can enter the systemd debug shell.

To do this, at the systemd-boot menu, hit the e key and specify: systemd.debug-shell=1.

In the future you can do this in configuration.nix:

systemd.additionalUpstreamSystemUnits = [ "debug-shell.service" ];
CMCDragonkai /
Last active Feb 18, 2021
Flexbox Sidebar with Overflowing Grid Items #css #flexbox #grid

Flexbox Sidebar with Overflowing Grid Items

Most flexbox layouts work by using the intrinsic sizing.

This means the sizes of boxes are determined by the size of the contents.

This is usually a good thing because you want your layout to stretch to the content, but also to the available space.

This makes layouts "flexible".

CMCDragonkai / async.js
Created Feb 4, 2021
Synchronous and Asynchronous Node.js Scripts #nodejs #javascript
View async.js
#!/usr/bin/env node
import process from 'process';
async function main(argv = process.argv): Promise<number> {
process.exitCode = 0;
return process.exitCode;
if (require.main === module) {
CMCDragonkai /
Created Jan 21, 2021
Python AsyncIO with Multithreading and Multiprocessing #python
import time
import asyncio
import logging
import uvloop
import aiojobs.aiohttp
import sys
from aiohttp import web
import aioprocessing
import concurrent.futures
CMCDragonkai /
Last active Jan 1, 2021
Combine Discounts #python

How to Combine Discounts

Suppose you have $1000 dollars, and you are applying 40% discount first, then 42% discount second.

What is the total discount?

1000 - 0.4*1000 - (0.42 * (1000 - 0.4*1000)) = 348
I - D1*I - (D2 * (I - D2*I)) = O
I - D1*I - D2*I + D1*D2*I = O
CMCDragonkai / Ref.vue
Last active Dec 23, 2020
Vue 3 Reactivity #vue
View Ref.vue
<button @click="counter += 1">{{ counter }}</button>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
setup () {
// by using ref, the counter becomes a proxy