Skip to content

Instantly share code, notes, and snippets.

@lagagain
Created January 2, 2020 04:28
Show Gist options
  • Save lagagain/034a9d32ee5488310d0240f7841b8b46 to your computer and use it in GitHub Desktop.
Save lagagain/034a9d32ee5488310d0240f7841b8b46 to your computer and use it in GitHub Desktop.
Python Proxy
class Proxy:
__handler = None
def __init__(self, handler:object):
self.__handler = handler
@classmethod
def Cproxy(clz, opt:str, default_handler = None):
def _handler(self, *args, **kwargs):
handler = getattr(self.__handler, opt, default_handler if default_handler else clz.__defaultOpt)
return handler(*args, **kwargs)
return _handler
def __defaultOpt(self, opt:str, *args, **kwargs):
raise ProxyOptException(opt)
for opt in operator.__dict__.keys():
if opt in ["__name__"]:continue
setattr(Proxy, opt, Proxy.Cproxy(opt))
'''Example
p = Proxy(1)
p + 1 # 2
p * 10 # 10
p / 2 # 0.5
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment