Предизвикателства > Abomination decorator > Решения > Решението на Илиана Миладинова

Резултати
2 точки от тестове
0 точки от учител

2 точки общо

4 успешни теста
0 неуспешни теста
Код

 1def check_input_arguments(args, types):
 2    for arg in args:
 3        if not isinstance(arg, types):
 4            print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types)))
 5            return False
 6    return True
 7        
 8
 9def type_check(val):
10    """Check the types of the input and output of a function."""
11
12    def wrapper(*types):
13        def decorator(func):
14            def check_types(*args, **kwargs):
15                if isinstance(val, str):
16                    if val == 'in':
17                        check_input_arguments(args,types)
18
19                    res = func(*args, **kwargs)
20
21                    if val == 'out':
22                        if not isinstance(res, types):
23                            print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types)))
24                
25                return res
26            return check_types
27        return decorator
28    return wrapper

....
----------------------------------------------------------------------
Ran 4 tests in 0.002s

OK

Дискусия
История

t1def check_input_arguments(args, types):t1def check_input_arguments(args, types):
2    for arg in args:2    for arg in args:
3        if not isinstance(arg, types):3        if not isinstance(arg, types):
4            print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types)))4            print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types)))
5            return False5            return False
6    return True6    return True
7        7        
88
9def type_check(val):9def type_check(val):
10    """Check the types of the input and output of a function."""10    """Check the types of the input and output of a function."""
1111
12    def wrapper(*types):12    def wrapper(*types):
13        def decorator(func):13        def decorator(func):
14            def check_types(*args, **kwargs):14            def check_types(*args, **kwargs):
15                if isinstance(val, str):15                if isinstance(val, str):
16                    if val == 'in':16                    if val == 'in':
17                        check_input_arguments(args,types)17                        check_input_arguments(args,types)
1818
19                    res = func(*args, **kwargs)19                    res = func(*args, **kwargs)
2020
21                    if val == 'out':21                    if val == 'out':
22                        if not isinstance(res, types):22                        if not isinstance(res, types):
23                            print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types)))23                            print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types)))
24                24                
25                return res25                return res
26            return check_types26            return check_types
27        return decorator27        return decorator
28    return wrapper28    return wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def check_input_arguments(args, types):f1def check_input_arguments(args, types):
2    for arg in args:2    for arg in args:
3        if not isinstance(arg, types):3        if not isinstance(arg, types):
4            print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types)))4            print('Invalid input arguments, expected {}!'.format(', '.join(str(type) for type in types)))
5            return False5            return False
6    return True6    return True
7        7        
88
9def type_check(val):9def type_check(val):
n10    """Check the types of the input and output of a fuction."""n10    """Check the types of the input and output of a function."""
1111
12    def wrapper(*types):12    def wrapper(*types):
13        def decorator(func):13        def decorator(func):
14            def check_types(*args, **kwargs):14            def check_types(*args, **kwargs):
15                if isinstance(val, str):15                if isinstance(val, str):
16                    if val == 'in':16                    if val == 'in':
17                        check_input_arguments(args,types)17                        check_input_arguments(args,types)
1818
19                    res = func(*args, **kwargs)19                    res = func(*args, **kwargs)
2020
21                    if val == 'out':21                    if val == 'out':
22                        if not isinstance(res, types):22                        if not isinstance(res, types):
23                            print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types)))23                            print('Invalid output value, expected {}!'.format(', '.join(str(type) for type in types)))
tt24                
24                    return res25                return res
25            return check_types26            return check_types
26        return decorator27        return decorator
27    return wrapper28    return wrapper
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op