Skip to content

Instantly share code, notes, and snippets.

View anatoly-kussul's full-sized avatar

Anatoly Kussul anatoly-kussul

View GitHub Profile
@anatoly-kussul
anatoly-kussul / sync_async_deco.py
Last active March 1, 2024 16:27
Python sync-async decorator factory
class SyncAsyncDecoratorFactory:
"""
Factory creates decorator which can wrap either a coroutine or function.
To return something from wrapper use self._return
If you need to modify args or kwargs, you can yield them from wrapper
"""
def __new__(cls, *args, **kwargs):
instance = super().__new__(cls)
# This is for using decorator without parameters
if len(args) == 1 and not kwargs and (inspect.iscoroutinefunction(args[0]) or inspect.isfunction(args[0])):