Skip to content

Instantly share code, notes, and snippets.

View whchi's full-sized avatar
:octocat:

whchi whchi

:octocat:
View GitHub Profile
@whchi
whchi / multi_process.py
Created November 4, 2020 08:31
python 多進程範例
from functools import partial
from multiprocessing import Pool, cpu_count
def batch(iterable, n=1):
iter_len = len(iterable)
for ndx in range(0, iter_len, n):
yield iterable[ndx:min(ndx + n, iter_len)]
@whchi
whchi / get_full_path_of_folders.py
Last active November 4, 2020 08:34
get nested folder full file path
import os
def get_file_list(src_dir):
input_path = []
for dir_path, subdir, filenames in os.walk(src_dir):
for f in filenames:
input_path.append(os.path.join(dir_path, f))
return input_path
@whchi
whchi / redis-sentinel-dkc.yml
Created December 9, 2020 07:50
docker-compose.yml example of redis sentienel
version: '3.8'
services:
### Redis Master ################################################
redis-master:
image: redis
restart: unless-stopped
ports:
- 6381:6379
networks:
local_network:
@whchi
whchi / main.py
Created December 9, 2020 10:22
fastapi custom middleware
from fastapi import FastAPI
from my_middleware import MyMiddleware
app = FastAPI()
app.add_middleware(JsonResponseFormatter)
@whchi
whchi / fastapi_422_exception_wrapper.py
Created December 16, 2020 06:20
fastapi exception handle
from fastapi.exceptions import RequestValidationError
from fastapi import FastAPI, Request, status
from fastapi.responses import JSONResponse
from app.routes import my_route
app = FastAPI()
# 包覆預設的 422 exception
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request,
exc: RequestValidationError):
@whchi
whchi / settings.py
Created December 16, 2020 08:14
use lru_cache for settings
from functools import lru_cache
import os
from dotenv import load_dotenv
from pathlib import Path
from typing import Any
BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(dotenv_path=f'{BASE_DIR}/app/.env')
@whchi
whchi / text_sanitizer.py
Created December 18, 2020 09:20
字串消毒,去除 html tag、control character、space 以及全形轉半形
# coding=utf-8
from html.parser import HTMLParser
from html import unescape
import unicodedata
import re
class MLStripper(HTMLParser):
"""
@whchi
whchi / retry.py
Created December 24, 2020 04:37
retry decorator
import time
def retry(exceptions: tuple, times=5, interval=10):
def wrapper(f):
def do(*args, **kwargs):
attempt = 0
while attempt < times:
try:
return f(*args, **kwargs)
@whchi
whchi / use_decorator.py
Created December 24, 2020 06:53
python singleton examples
def singleton(class_):
instances = {}
def get_instance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return get_instance
@whchi
whchi / docker-compose.yml
Created December 30, 2020 03:26
influxdb docker-compose volume setting
version: '3.9'
services:
influxdb:
image: quay.io/influxdb/influxdb:v2.0.2
ports:
- 8086:8086
########################################### solid volume
volumes:
- ~/docker/mount/var/lib/influxdb:/var/lib/influxdb
command: influxd run --bolt-path /var/lib/influxdb/influxd.bolt --engine-path /var/lib/influxdb/engine