Skip to content

Instantly share code, notes, and snippets.

View melvinkcx's full-sized avatar

Melvin Koh melvinkcx

View GitHub Profile
@melvinkcx
melvinkcx / container_definitions.tmpl
Created September 16, 2020 08:55
ECS Task Definition With SSM Params Injected Into `container_definition.tmpl`
[
{
"name": "your_app",
"image": "${image_repo_arn}:${image_tag}",
"essential": true,
"memoryReservation": 300,
"portMappings": [
{
"hostPort": 80,
"containerPort": 8000,
@melvinkcx
melvinkcx / scraper.py
Created August 18, 2020 06:13
Run Scrapy Crawler In A Script (Without Project)
import logging
from scrapy import signals
from scrapy.crawler import Crawler
from twisted.internet import reactor
from xxx.scraper.spiders import XXXSpider
logger = logging.getLogger(__name__)
@melvinkcx
melvinkcx / global_errors_in_react.md
Last active January 21, 2022 19:25
Handling Errors Globally In React

Handling Global Errors In React

My Approach: React Context with a custom useError hook

Even though React provides Error Boundaries API, it is not a good candidate, nor is relevant for handling GraphQL query errors.

useError custom hook

import { useState, useCallback } from 'react';
@melvinkcx
melvinkcx / assertion.py
Created May 20, 2020 07:46
Testing Django Signals Emission
from collections import Mapping
from dataclasses import dataclass
from typing import Any, Tuple
@dataclass()
class SignalHandlerContext:
sender: Any
args: Tuple[Any]
kwargs: Mapping
@melvinkcx
melvinkcx / interactive_docker.md
Created January 10, 2020 02:57
Docker Compose Interactive Mode for Debugging

Interactive Docker Container for Debugging with Compose

In order for PDB to work in a Docker container, you can use

docker run -it <image_name>

For Docker Compose, add two lines stdin_open: true and tty: true into the service of your docker-compose.yml:

@melvinkcx
melvinkcx / profiler_decorator.py
Last active December 23, 2019 05:53
A decorator to run profiler in Python
"""
with Python 3.8, you can use context manager with cProfile.Profile()
"""
def profile(func, filename="stats"):
def wrapper(*args, **kwargs):
import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
@melvinkcx
melvinkcx / index.js
Created December 22, 2019 07:56
Yielding Python Generator from JS in PyWebView: A Dummy Example
async *genLogs() {
while (true) {
yield await window.pywebview.api.gen_logs();
}
}
const g = genLogs();
(await g.next()).value // 1
(await g.next()).value // 2
@melvinkcx
melvinkcx / implementation_1_busy_waiting.py
Last active November 23, 2019 11:38
Demo - Producer/Consumer Problem
"""
Implementation 1: Infinite Loop in Consumers
The problem:
the serving line is busy waiting
"""
import queue
import threading
@melvinkcx
melvinkcx / using_filterable_dict.py
Last active September 26, 2020 15:25
Filtering Dictionary in Python
from typing import Dict
# Helper function to generate dict object
def get_menu() -> Dict[str, dict]:
return {
"TIMMY_BLACK": {
"item": "Timmy's Coffee Barista's Black",
"sugar_free": True,
"with_milk": False,
},
@melvinkcx
melvinkcx / filterable_dict.py
Last active June 30, 2019 07:37
Filtering Dictionary in Python
class FilterableDict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
def filter(self, predicate):
key_copy = tuple(self.keys()) # use Tuple to further reduce memory footprint
for k in key_copy:
if predicate(k, self.get(k)):
del self[k]
return self