Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Lightspeed

Roger Qiu CMCDragonkai

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

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
CMCDragonkai / understanding_systemd_boot_execution_graph.md
Last active Mar 30, 2021
Understanding systemd Boot Execution Graph #systemd
View understanding_systemd_boot_execution_graph.md
@CMCDragonkai
CMCDragonkai / npm_clean.md
Created Mar 16, 2021
NPM Clean - Needed if SHA1 hashes are appearing #npm
View npm_clean.md

NPM Clean - Needed if SHA1 hashes are appearing

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

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
CMCDragonkai / flexbox_sidebar_overflowing_grid_items.md
Last active Feb 18, 2021
Flexbox Sidebar with Overflowing Grid Items #css #flexbox #grid
View flexbox_sidebar_overflowing_grid_items.md

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
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
CMCDragonkai / asyncio_multithreading_multiprocessing.py
Created Jan 21, 2021
Python AsyncIO with Multithreading and Multiprocessing #python
View asyncio_multithreading_multiprocessing.py
import time
import asyncio
import logging
import uvloop
import aiojobs.aiohttp
import sys
from aiohttp import web
import aioprocessing
import concurrent.futures
@CMCDragonkai
CMCDragonkai / Derivation.md
Last active Jan 1, 2021
Combine Discounts #python
View Derivation.md

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
CMCDragonkai / Ref.vue
Last active Dec 23, 2020
Vue 3 Reactivity #vue
View Ref.vue
<template>
<button @click="counter += 1">{{ counter }}</button>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
setup () {
// by using ref, the counter becomes a proxy