Skip to content

Instantly share code, notes, and snippets.

@averykhoo
averykhoo / url_glob_pattern_matching.py
Created October 31, 2023 08:53
convert glob patterns to regex matchers, with support for globstar
import re
import warnings
from typing import Pattern
def translate_url_pattern(pattern: str, # noqa: max-complexity: 20
case_sensitive: bool = True,
) -> Pattern:
"""
translates a url pattern (like a glob pattern) to a regular expression
@averykhoo
averykhoo / get_function_name.py
Last active November 22, 2021 06:46
get module, class, function name
import inspect
from functools import lru_cache
from functools import partial
from typing import Callable
@lru_cache(maxsize=None)
def get_function_name(func: Callable) -> str:
"""
Get the name of a function.
@averykhoo
averykhoo / debouncing.py
Last active December 8, 2021 10:10
rate limiter
import datetime
from functools import wraps
from threading import Condition
from threading import Lock
from typing import Callable
from typing import Union
def debounce(timeout: Union[int, float, datetime.timedelta], *, default: Any = None) -> Callable:
"""
@averykhoo
averykhoo / bhanot_dictionary.py
Created November 10, 2020 07:15
malay-english dictionary words extracted from http://dictionary.bhanot.net/
"""
malay-english dictionary words extracted from http://dictionary.bhanot.net/
(extracted on 2020-11-10)
"""
definitions = {
'abad': 'century',
'berabad-abad': 'for centuries',
@averykhoo
averykhoo / zalgo.py
Last active October 8, 2020 06:25
various zalgo / unzalgo / is_zalgo functions
import random
import re
import unicodedata
RE_ZALGO = re.compile(r'(?:.[\u0300-\u036F\u0488\u0489]+)+(?:(?:\s+|[^\w])(?:.[\u0300-\u036F\u0488\u0489]+)+)*')
def sad_face(text: str):
# return text.replace('', '\u0311\u0308')[2:] # substitute whitespace
@averykhoo
averykhoo / capture-screen.vbs
Last active June 30, 2020 09:15
capture a screenshot using an excel macro
Option Explicit
'#######################################################################################
' Module code for capturing a screen image (Print Screen) and pasting to a new workbook
' Created on November 14th, 2009, compiled by Zack Barresse
' Compiled utilizing the following resources:
' http://www.ac6la.com/makegif.html
' http://www.andreavb.com/tip090001.html
'#######################################################################################
' Updated by Avery on 2020-06-23 to make the code work with 64-bit Excel
@averykhoo
averykhoo / sparkles.js
Last active July 6, 2023 05:22
Have a trail of sparkles following your mouse cursor (based on: https://codepen.io/doglol/pen/WNNBRmK)
const star = [];
const star_x = [];
const star_y = [];
const star_remaining_ticks = [];
const tiny = [];
const tiny_x = [];
const tiny_y = [];
const tiny_remaining_ticks = [];
const sparkles = 250; // total number of stars, same as number of dots
const sparkle_lifetime = 30; // each star lives for twice this, then turns into a dot that also lives twice this
{
"basics": {
"name": "Avery Khoo",
"label": "Senior Data Scientist",
"picture": "",
"email": "averykhoo@gmail.com",
"phone": "",
"degree": "B.Eng in Computer Science",
"website": "",
"summary": "I'm a data scientist, but I've also worked on devops platforms and infra. ",
from functools import wraps
def verbose(func):
"""Decorator to print function call details - parameters names and effective values"""
@wraps(func)
def wrapper(*func_args, **func_kwargs):
print('func_code.co_varnames =', func.func_code.co_varnames)
print('func_code.co_argcount =', func.func_code.co_argcount)
# https://en.wikipedia.org/wiki/National_Registration_Identity_Card
# http://www.ngiam.net/NRIC/
# http://www.ngiam.net/NRIC/NRIC_numbers.pdf
import random
weights = [2, 7, 6, 5, 4, 3, 2]
local_alphas = 'JZIHGFEDCBA'
foreign_alphas = 'XWUTRQPNMLK'