Skip to content

Instantly share code, notes, and snippets.

View ktmud's full-sized avatar
🥌
Peace and Love~

Jesse Yang ktmud

🥌
Peace and Love~
View GitHub Profile
@ktmud
ktmud / FlashingDots.tsx
Created September 8, 2022 05:13
Chakra UI FlashingDots
import {
chakra,
keyframes,
CSSObject,
ChakraProps,
useToken,
} from '@chakra-ui/react';
export type FlashingDotsProps = Pick<ChakraProps, 'width' | 'height'> & {
/**
@ktmud
ktmud / app.py
Created May 20, 2022 21:58
aiohttp server with file system based router
import pkgutil
from aiohttp import web
ALLOWED_METHODS = ["GET", "POST"]
def load_route_handlers(path, mod):
routes = []
for method in ALLOWED_METHODS:
if hasattr(mod, method):
@ktmud
ktmud / __init__.py
Last active January 27, 2024 16:19
An extreme simple Python http server with auto reload and file-system based router
import os
from .app import start_cli_service
env = (os.environ.get("env") or "prod").lower()
is_dev = env == "dev" or env == "local"
port, autoreload_observer = start_cli_service(autoreload=is_dev)
if autoreload_observer:
# move autoreload observer to the foreground so process won't exit
@ktmud
ktmud / sqlalchemy_insert_from_select.py
Last active March 29, 2022 22:15
Understand how SQLAlchemy insert().from_select(..) works
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Model(Base):
__tablename__ = "model"
id = sa.Column("id", primary_key=True)
bbb = sa.Column("bbb")
@ktmud
ktmud / FlashingDots.tsx
Created September 13, 2021 22:49
CSS Loading Indicator Three Flashing Dots in styled-jsx
export type FlashingDotsProps = {
/**
* Size of each dot
*/
dotSize?: number;
/**
* Animation duration.
*/
duration?: number;
/**
@ktmud
ktmud / remove_duplicate.py
Created March 14, 2021 06:57
Python Remove Duplicates by Key
def remove_duplicates(
items: Iterable[InputType], key: Optional[Callable[[InputType], Any]] = None
) -> List[InputType]:
"""Remove duplicate items in an iterable."""
if not key:
return list(dict.fromkeys(items).keys())
seen = set()
result = []
for item in items:
item_key = key(item)
@ktmud
ktmud / cancel_github_workflows.py
Last active January 9, 2023 19:59
Manually cancel previous GitHub Action workflow runs in queue
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
import {
ReactNode,
HTMLAttributes,
CSSProperties,
HTMLProps,
MutableRefObject,
FunctionComponent,
} from 'react';
declare module 'react-syntax-highlighter' {
@ktmud
ktmud / superset_exception.py
Last active July 14, 2020 16:38
API Design in Python/Flask - Generic Exceptions with Error Codes
from enum import Enum, IntEnum
from typing import Any, Dict, Optional
from flask import jsonify
from flask_babel import gettext as _, ngettext
from superset.typing import FlaskResponse
class SupersetErrorCode(IntEnum):
# Generic Superset errors
@ktmud
ktmud / concurrent.futures-intro.md
Created September 27, 2019 21:18 — forked from mangecoeur/concurrent.futures-intro.md
Easy parallel python with concurrent.futures

Easy parallel python with concurrent.futures

As of version 3.3, python includes the very promising concurrent.futures module, with elegant context managers for running tasks concurrently. Thanks to the simple and consistent interface you can use both threads and processes with minimal effort.

For most CPU bound tasks - anything that is heavy number crunching - you want your program to use all the CPUs in your PC. The simplest way to get a CPU bound task to run in parallel is to use the ProcessPoolExecutor, which will create enough sub-processes to keep all your CPUs busy.

We use the context manager thusly:

with concurrent.futures.ProcessPoolExecutor() as executor: