Skip to content

Instantly share code, notes, and snippets.

Melvin Koh melvinkcx

Block or report user

Report or block melvinkcx

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@melvinkcx
melvinkcx / interactive_docker.md
Created Jan 10, 2020
Docker Compose Interactive Mode for Debugging
View interactive_docker.md

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 / interactive_docker.md
Created Jan 10, 2020
Docker Compose Interactive Mode for Debugging
View interactive_docker.md

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 / interactive_docker.md
Created Jan 10, 2020
Docker Compose Interactive Mode for Debugging
View interactive_docker.md

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 Dec 23, 2019
A decorator to run profiler in Python
View profiler_decorator.py
"""
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 Dec 22, 2019
Yielding Python Generator from JS in PyWebView: A Dummy Example
View index.js
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 Nov 23, 2019
Demo - Producer/Consumer Problem
View implementation_1_busy_waiting.py
"""
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 Jun 30, 2019
Filtering Dictionary in Python
View using_filterable_dict.py
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 Jun 30, 2019
Filtering Dictionary in Python
View filterable_dict.py
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
@melvinkcx
melvinkcx / solution_deferred_update.py
Created Jun 30, 2019
Filtering Dictionary in Python
View solution_deferred_update.py
"""
Solution #3: Deferred Update
Risk: What if an Exception is raised half way?
"""
coffees = get_menu()
keys_to_be_removed = []
for code, details in coffees.items():
if details.get("sugar_free") is True:
keys_to_be_removed.append(code)
@melvinkcx
melvinkcx / solution_copy_of_dict_keys.py
Created Jun 30, 2019
Filtering Dictionary In Python 3
View solution_copy_of_dict_keys.py
"""
Solution #2: Create a copy of keys
"""
coffees = get_menu()
key_copy = tuple(coffees.keys()) # Feel free to use any iterable collection
for k in key_copy:
if coffees[k].get("sugar_free") is True:
del coffees[k]
You can’t perform that action at this time.