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

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

2 точки общо

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

 1def type_check(input_output):
 2    def compare_types(*expected_types):
 3        if input_output == "in":
 4            """ Decorate to check input types. """
 5            def compare_input(func):
 6                def decorated_input(*args, **kwargs):
 7                    """ Check for input type mismatch. """
 8                    is_valid_input = True
 9                    for arg in args:
10                        if not any(isinstance(arg, t) for t in expected_types):
11                            is_valid_input = False
12                            break
13                    
14                    if not is_valid_input:
15                        result_str = ', '.join(str(t) for t in expected_types)
16                        print(f"Invalid input arguments, expected {result_str}!")
17                        
18                    return func(*args, **kwargs)
19                return decorated_input
20            return compare_input
21        
22        elif input_output == "out":
23            """ Decorate to check output types. """
24            def compare_output(func):
25                def decorated_output(*args, **kwargs):
26                    """ Check for output type mismatch. """
27                    result = func(*args, **kwargs)
28                    
29                    if not any(isinstance(result, t) for t in expected_types):
30                        result_str = ', '.join(str(t) for t in expected_types)
31                        print(f"Invalid output value, expected {result_str}!")
32                    
33                    return result
34                return decorated_output
35            return compare_output
36    return compare_types

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

OK

Дискусия
История
Това решение има само една версия.