Created
May 17, 2016 15:29
-
-
Save 13steinj/07cf9d0c89bda8e85e658f2ee01f436d to your computer and use it in GitHub Desktop.
this porbably doesnt work i dont remember what I did for work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
from inspect import getfullargspec as gas | |
except ImportError: | |
from inspect import getargspec as gas | |
class UnannotatedArg(object): | |
def __eq__(self, other): | |
return True | |
class OverloadedMethod(object): | |
unannotatedarg = UnannotatedArg() | |
def __init__(self, default=None): | |
self.default = default | |
self.funcs = [] | |
@classmethod | |
def overloader(cls, default=None): | |
return cls(default) | |
def register(self, func): | |
self.funcs.append(func) | |
def __call__(self, *args, **kwargs): | |
scores = {func: 0 for func in self.funcs} | |
for func in self.funcs: | |
listed_args = [argument for argument in gas(func)[0] if argument not in kwargs] | |
mapper = dict(zip(listed_args, args), kwargs) | |
if all(func.__annotations__.get(k, self.unannotatedarg) == type(v) for k, v in mapper): | |
return func(*args, **kwargs) | |
else: | |
if self.default: | |
return self.default(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment