-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
39 lines (30 loc) · 1.01 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Much cheaper version of 'overloaded', based exclusively on number of args"""
# TODO
# add support for class-, -static methods and functions
class Grouped(dict):
def __call__(self, key):
def wrap(value):
self[key] = value
return self
return wrap
def group(key):
return lambda value: Grouped({key: value})
class ArityGrouped(dict):
def __call__(self, f):
self[f.__code__.co_argcount] = f
return self
def with_arity(f):
return ArityGrouped()(f)
class Arity(type):
def __new__(cls, name, bases, attrs):
for k, v in attrs.items():
if isinstance(v, ArityGrouped):
def wrap(*args):
try:
f = v[len(args)]
except KeyError:
raise TypeError(f'no function with arity {len(args)}')
else:
return f(*args)
attrs[k] = wrap
return super().__new__(cls, name, bases, attrs)