Skip to content

Instantly share code, notes, and snippets.

View linw1995's full-sized avatar
🎯
Focusing

林玮 (Jade Lin) linw1995

🎯
Focusing
View GitHub Profile
@linw1995
linw1995 / utils.py
Last active March 24, 2021 07:16
Enum class for creating enum members with name and value being same.
import enum
class AutoValue(enum.Enum):
# is same with the example of docs.python.org
# https://docs.python.org/3/library/enum.html#using-automatic-values
def _generate_next_value_(name, start, count, last_values):
return name
@linw1995
linw1995 / demo.py
Last active March 10, 2021 07:39
Click with optional arguments which are enum type.
# Standard Library
import enum
# Third Party Library
import click
class Order(enum.Enum):
latest = "latest"
oldest = "oldest"
@linw1995
linw1995 / example.py
Created March 9, 2021 08:19
gracefully shutdown the top asyncio coroutine in Python
import asyncio
import signal
import logging
import contextlib
logger = logging.getLogger(__name__)
def asyncio_run(coro):
task = asyncio.ensure_future(coro)
@linw1995
linw1995 / cmd_with_retry.py
Last active May 9, 2021 13:09
Simple script for executing command with retry
import os
import subprocess
import sys
def run_cmd_with_retry(
cmd,
retry_count_limit=3,
retry_delay_seconds=60,
stdout=sys.stdout.fileno(),
@linw1995
linw1995 / main.py
Created February 23, 2021 08:00
Python asyncio.Semaphore with status hooking
import asyncio
import contextlib
import blinker
class Worker:
def __init__(self, name, count_task_limit):
self.name = name
self.count_task_limit = count_task_limit
@linw1995
linw1995 / demo.py
Last active March 10, 2021 07:59
MustSetProperty -- descriptor that raises exception if its value is None.
from typing import Any, Generic, Optional, TypeVar, Union, overload
T = TypeVar("T")
class UnsetError(Exception):
pass
class MustSetProperty(Generic[T]):
@linw1995
linw1995 / README.md
Last active June 6, 2021 06:51
Please don't use ContextVar.reset in async_generator.

Please don't use ContextVar.reset in async_generator.

Important rule: Different coroutines own different contexts.

  • The example1.py runs one coroutine with async_generator, the async_generator.__anext__ method runs in same context and thus it works find.
  • The example2.py produces two coroutines with the same async_generator, the async_generator.__anext__ method runs in different contexts and thus it raises an ValueError exception.

Tips for async_generator with ContextVar

@linw1995
linw1995 / README.md
Last active December 25, 2020 06:28
Parse code of JavaScript to convert literal statement like preloaded state of Redux.js into python data.
@linw1995
linw1995 / .emacs
Created June 20, 2020 06:51
My Simple Emacs Configure
;;; Package Managing
;; Bootstrapping straight.el
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
@linw1995
linw1995 / poetry.lock
Last active May 20, 2020 14:20
Export wrong dependencies when using optional=true
[[package]]
category = "main"
description = "Atomic file writes."
marker = "sys_platform == \"win32\""
name = "atomicwrites"
optional = true
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
version = "1.4.0"
[[package]]