Предизвикателства > Abomination decorator > Решения > Решението на Йоан Байчев

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

2 точки общо

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

 1def identity_function(function):
 2    return function
 3
 4def type_check(arg_type):
 5    if type(arg_type) is not str or (arg_type != "in" and arg_type != "out"):
 6        print("Error!!!")
 7        return identity_function
 8
 9    def type_decorator(*expected_types):
10
11        def type_wrapper(function):
12
13            def type_validator(*args, **kwargs):
14
15                def print_error():
16                    expected_types_str = ', '.join(map(str, expected_types))
17                    if arg_type == "in":
18                        print(f"Invalid input arguments, expected {expected_types_str}!")
19                    elif arg_type == "out":
20                        print(f"Invalid output value, expected {expected_types_str}!")
21
22                if arg_type == "in":
23                    invalid_args = [
24                        current_arg for current_arg in args
25                        if type(current_arg) not in expected_types
26                    ]
27                    invalid_kwargs = [
28                        current_value for current_value in kwargs.values()
29                        if type(current_value) not in expected_types
30                    ]
31                    if invalid_args or invalid_kwargs:
32                        print_error()
33
34                result = function(*args, **kwargs)
35
36                if arg_type == "out" and type(result) not in expected_types:
37                    print_error()
38
39                return result
40
41            return type_validator
42
43        return type_wrapper
44
45    return type_decorator

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

OK

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

nn1def identity_function(function):
2    return function
3 
1def type_check(arg_type):4def type_check(arg_type):
nn5    if type(arg_type) is not str or (arg_type != "in" and arg_type != "out"):
6        print("Error!!!")
7        return identity_function
28
3    def type_decorator(*expected_types):9    def type_decorator(*expected_types):
410
5        def type_wrapper(function):11        def type_wrapper(function):
612
7            def type_validator(*args, **kwargs):13            def type_validator(*args, **kwargs):
814
9                def print_error():15                def print_error():
10                    expected_types_str = ', '.join(map(str, expected_types))16                    expected_types_str = ', '.join(map(str, expected_types))
11                    if arg_type == "in":17                    if arg_type == "in":
12                        print(f"Invalid input arguments, expected {expected_types_str}!")18                        print(f"Invalid input arguments, expected {expected_types_str}!")
13                    elif arg_type == "out":19                    elif arg_type == "out":
14                        print(f"Invalid output value, expected {expected_types_str}!")20                        print(f"Invalid output value, expected {expected_types_str}!")
1521
16                if arg_type == "in":22                if arg_type == "in":
t17                    invalid_args = [current_arg for current_arg in args if type(current_arg) not in expected_types]t23                    invalid_args = [
18                    invalid_kwargs = [current_value for current_value in kwargs.values() if type(current_value) not in expected_types]24                        current_arg for current_arg in args
25                        if type(current_arg) not in expected_types
26                    ]
27                    invalid_kwargs = [
28                        current_value for current_value in kwargs.values()
29                        if type(current_value) not in expected_types
30                    ]
19                    if invalid_args or invalid_kwargs:31                    if invalid_args or invalid_kwargs:
20                        print_error()32                        print_error()
2133
22                result = function(*args, **kwargs)34                result = function(*args, **kwargs)
2335
24                if arg_type == "out" and type(result) not in expected_types:36                if arg_type == "out" and type(result) not in expected_types:
25                    print_error()37                    print_error()
2638
27                return result39                return result
2840
29            return type_validator41            return type_validator
3042
31        return type_wrapper43        return type_wrapper
3244
33    return type_decorator45    return type_decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op