Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import inspect
  2. import asyncio
  3. class AsyncClassWrapper:
  4.     def __init__(self, wrapped):
  5.         self.wrapped = wrapped
  6.  
  7.     def __getattr__(self, item):
  8.         w = getattr(self.wrapped, item)
  9.  
  10.         async def wrapper(*args,**kwargs):
  11.             val = w(self.wrapped, *args, **kwargs)
  12.             return await val if inspect.isawaitable(val) else val
  13.         if inspect.isfunction(w):
  14.             return wrapper
  15.         elif isinstance(w,property):
  16.             return w.fget(self.wrapped)
  17.         else:
  18.             return w
  19.  
  20.  
  21. class session:
  22.     test1='test1'
  23.     async def test2(self,arg):
  24.         print(arg)
  25.  
  26.     async def test3(self,arg):
  27.         print("test3")
  28.  
  29.     @property
  30.     def test4(self):
  31.         return "test4"
  32.  
  33. async def t():
  34.     s = AsyncClassWrapper(session)
  35.     print(s.test1)
  36.     await s.test2("test2")
  37.     await s.test3("k")
  38.     print(s.test4)
  39.     s.test5 = "test5"
  40.     print(s.test5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement