Предизвикателства > Abomination decorator > Решения > Решението на Димитър Танков

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

2 точки общо

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

 1def type_check(mode):
 2    def get_types(*types):
 3        def validate_corresponding_types(func):
 4            def wrapper(*args, **kwargs):
 5                output_value = 0
 6
 7                if mode == "in":
 8                    arg_list = (list(map(lambda x: ('', x), args))
 9                                + list(kwargs.items()))
10
11                    for _, value in arg_list:
12                        if not isinstance(value, types):
13                            formatted_types = ", ".join([str(val_type)
14                                                        for val_type in types])
15
16                            print(f"Invalid input arguments, expected "
17                                  f"{formatted_types}!")
18                            break
19
20                    output_value = func(*args, **kwargs)
21
22                elif mode == "out":
23                    output_value = func(*args, **kwargs)
24
25                    if not isinstance(output_value, types):
26                        formatted_types = ", ".join([str(val_type)
27                                                    for val_type in types])
28
29                        print(f"Invalid output value, expected "
30                              f"{formatted_types}!")
31
32                return output_value
33            return wrapper
34        return validate_corresponding_types
35    return get_types

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

OK

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

f1def type_check(mode):f1def type_check(mode):
2    def get_types(*types):2    def get_types(*types):
3        def validate_corresponding_types(func):3        def validate_corresponding_types(func):
4            def wrapper(*args, **kwargs):4            def wrapper(*args, **kwargs):
5                output_value = 05                output_value = 0
66
7                if mode == "in":7                if mode == "in":
8                    arg_list = (list(map(lambda x: ('', x), args))8                    arg_list = (list(map(lambda x: ('', x), args))
9                                + list(kwargs.items()))9                                + list(kwargs.items()))
1010
t11                    for key, value in arg_list:t11                    for _, value in arg_list:
12                        if not isinstance(value, types):12                        if not isinstance(value, types):
13                            formatted_types = ", ".join([str(val_type)13                            formatted_types = ", ".join([str(val_type)
14                                                        for val_type in types])14                                                        for val_type in types])
1515
16                            print(f"Invalid input arguments, expected "16                            print(f"Invalid input arguments, expected "
17                                  f"{formatted_types}!")17                                  f"{formatted_types}!")
18                            break18                            break
1919
20                    output_value = func(*args, **kwargs)20                    output_value = func(*args, **kwargs)
2121
22                elif mode == "out":22                elif mode == "out":
23                    output_value = func(*args, **kwargs)23                    output_value = func(*args, **kwargs)
2424
25                    if not isinstance(output_value, types):25                    if not isinstance(output_value, types):
26                        formatted_types = ", ".join([str(val_type)26                        formatted_types = ", ".join([str(val_type)
27                                                    for val_type in types])27                                                    for val_type in types])
2828
29                        print(f"Invalid output value, expected "29                        print(f"Invalid output value, expected "
30                              f"{formatted_types}!")30                              f"{formatted_types}!")
3131
32                return output_value32                return output_value
33            return wrapper33            return wrapper
34        return validate_corresponding_types34        return validate_corresponding_types
35    return get_types35    return get_types
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op