Skip to content

Instantly share code, notes, and snippets.

@13steinj
Created May 17, 2016 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13steinj/07cf9d0c89bda8e85e658f2ee01f436d to your computer and use it in GitHub Desktop.
Save 13steinj/07cf9d0c89bda8e85e658f2ee01f436d to your computer and use it in GitHub Desktop.
this porbably doesnt work i dont remember what I did for work
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