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

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

2 точки общо

3 успешни теста
1 неуспешни теста
Код

 1def type_check(stream_type):
 2    def allow_types(*args):
 3        allowed_types = set()
 4        for variable in args:
 5            allowed_types.add(type(variable()))
 6        def print_if_incorrect(func):
 7            def wrapper(*args, **kwargs):
 8                if stream_type == "in":
 9                    correct_inputs = True
10                    for current_input in (list(args) + list(kwargs.values())):
11                        if type(current_input) not in allowed_types:
12                            correct_inputs = False
13                            break
14                    if not correct_inputs:
15                        print("Invalid input arguments, expected {}!".format(
16                            ", ".join(str(elem) for elem in allowed_types)))
17                result_of_func = func(*args, **kwargs)
18                if stream_type == "out":
19                    if type(result_of_func) not in allowed_types:
20                        print("Invalid output value, expected {}!".format(
21                             ", ".join(str(elem) for elem in allowed_types)))
22                return result_of_func
23            return wrapper
24        return print_if_incorrect
25    return allow_types

..E.
======================================================================
ERROR: test_check_in (test.TestTypeCheck.test_check_in)
The decorator should report invalid "in" arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.12/unittest/mock.py", line 1390, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 51, in test_check_in
decorated = type_check("in")(*types)(join_collections)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 5, in allow_types
allowed_types.add(type(variable()))
^^^^^^^^^^
TypeError: cannot create 'generator' instances

----------------------------------------------------------------------
Ran 4 tests in 0.003s

FAILED (errors=1)

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