Skip to content

Instantly share code, notes, and snippets.

View melvinkcx's full-sized avatar

Melvin Koh melvinkcx

View GitHub Profile
melvinkcx / release.yml
Created December 23, 2022 06:52
Github Workflow To Create A Release Using Poetry On Tag
name: Release
- "v*.*.*"
runs-on: ubuntu-latest
melvinkcx /
Created November 18, 2022 04:35
run_with_wait doesn't cancel but wait for all tasks to be completed when the main task is completed, unlike
from asyncio import coroutines, events, tasks
def run_with_wait(main, *, debug=None):
Difference from
- cancels all tasks when `main` is completed.
any tasks created using `asyncio.create_task()` will hence be cancelled.
- run_with_wait() waits for all tasks to be completed using
`run_until_complete()` and `asyncio.tasks.gather()`
melvinkcx / Dockerfile
Created October 12, 2022 07:05
Multi-stage Python image with Poetry as deps manager
FROM python:3.10 AS base
RUN curl -sSL | python3 -
COPY ./poetry.lock /app/poetry.lock
COPY ./pyproject.toml /app/pyproject.toml
melvinkcx /
Created May 25, 2022 02:14
SQLAlchemy Introspect API - Introspecting models using async connection
import logging
from sqlalchemy import MetaData, Table
from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker
logger = logging.getLogger(__name__)
meta = MetaData()
melvinkcx /
Last active May 23, 2022 08:39
Bundle Python app managed with Poetry for AWS lambda
OLDPWD=$(pwd) \
&& cd "$(poetry env info --path)/lib/python3.9/site-packages" \
&& zip -r9 ./ . -x "*.pyc" \
&& mv ./ $OLDPWD \
&& cd $OLDPWD \
&& zip -rg ./ ./app \
&& aws lambda update-function-code --function-name my_function --zip-file fileb://
# A better alternative is to first upload to an S3 bucket, then update the function
melvinkcx /
Last active January 13, 2022 05:54
Converting Unicode Sentences To Snake Case In SQL and Python
import re
import unidecode
def to_snake_case(text):
Convert unicode text to snake case
>>> to_snake_case("My favourite dish was raclette.")
melvinkcx /
Created February 15, 2021 09:03
Configurable FastAPI TestClient To Allow Us To Set Cookies Once And For All
from starlette.testclient import TestClient
class CookieConfigurableTestClient(TestClient):
_access_token = None
def set_access_token(self, token):
self._access_token = token
def reset(self):
melvinkcx /
Last active November 20, 2020 06:47
Crafting CloudWatch Logs Lambda Event
import base64
import gzip
import json
event = {
"logGroup": "/ecs/melvin-dev",
"logStream": "ecs/melvin-dev/XXXXXXXXXX",
"owner": 100000000000,
"logEvents": [{
melvinkcx /
Last active September 18, 2020 07:34
Terraform Provisioner: Wait For ECS To Register With LB Before Destroying
# Ref:
resource "aws_ecs_service" "web" {
name = "web_service_${var.environment}_${replace(timestamp(), ":", "-")}"
cluster =
task_definition = aws_ecs_task_definition.web.arn
desired_count = 1
force_new_deployment = true
melvinkcx /
Created September 17, 2020 14:26
Configure Celery To Write Logs To Papertrail
import logging
import os
from celery import Celery
from celery.schedules import crontab
from celery.signals import after_setup_logger, after_setup_task_logger
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")