-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.py
More file actions
executable file
·40 lines (34 loc) · 1.28 KB
/
templates.py
File metadata and controls
executable file
·40 lines (34 loc) · 1.28 KB
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
40
#!/usr/bin/env python3
def are_args_of_types(list_args, list_types):
"""
are_args_of_types([4, 1.0], [int, float]) -> True
are_args_of_types([4, 1.0], [int, int]) -> False
are_args_of_types([4, 1], [int, float]) -> False
are_args_of_types([4, 1.0], [int, (int, float)]) -> True
are_args_of_types([4, 1], [int, (int, float)]) -> True
"""
return all(any(isinstance(a, tt) for tt in t) if isinstance(t, set) else
isinstance(a, t)
for a, t in zip(list_args, list_types))
from collections import defaultdict
import inspect
template_funcs = defaultdict(list)
def template(f):
argspec = inspect.getfullargspec(f)
template_funcs[f.__name__].append(([argspec.annotations[a] if a in argspec.annotations else object for a in argspec.args], f))
def decorator(*args, **kwargs):
for type_args, f_ in template_funcs[f.__name__]:
if are_args_of_types(args, type_args):
return f_(*args, **kwargs)
return decorator
@template
def addition(a : {int, float}, b : {int, float}):
print('Addition int')
return a + b
@template
def addition(a : str, b : str):
print('Addition str')
return a + b
print(addition(5, 4))
print(addition('a', 'b'))
print(addition(5.0, 1.0))