Skip to content

Instantly share code, notes, and snippets.

View falkben's full-sized avatar
🚲

Ben Falk falkben

🚲
View GitHub Profile
@falkben
falkben / sync_async_deco.py
Created August 1, 2020 04:07 — forked from anatoly-kussul/sync_async_deco.py
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])):