Skip to content

Instantly share code, notes, and snippets.

View kakarukeys's full-sized avatar

Wong Jiang Fung kakarukeys

View GitHub Profile
@kakarukeys
kakarukeys / .bash_profile
Last active December 4, 2021 00:50
shell script to enable Poetry .env loading
function poetry() {
dot_env_path=$(pwd)
while [[ "$dot_env_path" != "" && ! -e "$dot_env_path/.env" ]]; do
dot_env_path=${dot_env_path%/*}
done
# if POETRY_DONT_LOAD_ENV is *not* set, then load .env if it exists
if [[ -z "$POETRY_DONT_LOAD_ENV" && -f "$dot_env_path/.env" ]]; then
>&2 echo 'Loading .env environment variables…'
from collections import defaultdict
from asyncio import Semaphore
MAX_CONNECTIONS_PER_MX = 1
leaky_bucket = defaultdict(lambda: Semaphore(MAX_CONNECTIONS_PER_MX))
async def ping_server_handler(request):
...
@kakarukeys
kakarukeys / solution2.js
Created May 21, 2017 13:51
solution2 - URI comparison
function parseURI(uri) {
/* parse <uri> into invidual components (loosely, some errors allowed) */
// see https://regex101.com/r/l96l7F/1 for explanation
var regex = /(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?((?:\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?[^?#\/]*)(?:\?([^#]*))?(?:#(.*))?/,
groupNames = [
"fullMatch",
"scheme",
"username",
"password",
@kakarukeys
kakarukeys / solution1.js
Created May 21, 2017 13:49
solution1 - string compression
// if performance is not important
function compress(str) {
/* shorten <str> by reducing consecutive character repetitions */
return str.replace(/(.)\1*/g, function(fullMatch, g1) {
return g1 + fullMatch.length;
});
}
console.assert(compress('') === '', "blank string");
console.assert(compress('a') === "a1", "single char");