Skip to content

Instantly share code, notes, and snippets.

@jacek-jablonski
Created June 25, 2024 15:19
Show Gist options
  • Save jacek-jablonski/5b23d34e42030ab68cfa4e5576c016a8 to your computer and use it in GitHub Desktop.
Save jacek-jablonski/5b23d34e42030ab68cfa4e5576c016a8 to your computer and use it in GitHub Desktop.
from collections.abc import Callable, Collection
from typing import ParamSpec, TypeVar
R = TypeVar("R")
P = ParamSpec("P")
def disable(
func_or_metrics: Collection[int] | Callable[P, R]
) -> Callable[[Callable[P, R]], Callable[P, R]] | Callable[P, R]:
def _disable(f: Callable[P, R], metrics: Collection[int]) -> None:
if metrics:
print(metrics)
if callable(func_or_metrics):
_disable(func_or_metrics, [])
return func_or_metrics
else:
def wrapper(f: Callable[P, R]) -> Callable[P, R]:
_disable(f, func_or_metrics)
return f
return wrapper
@disable
def _test_endpoint1() -> str:
return "Hello"
@disable([1])
def _test_endpoint2() -> str:
return "Hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment