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

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

2 точки общо

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

 1def type_check(mode):
 2    def inject_parameters(*args):
 3        def modified_func(func):
 4            def parameters(*positional_args, **keyword_args):
 5                if mode == "in":
 6                    expected_parameters = set(filter(lambda x: (type(x) not in args), positional_args))
 7                    expected_parameters |= set(filter(lambda x: (type(x) not in args), list(keyword_args.values())))
 8                    
 9                    if len(expected_parameters) != 0:
10                        print("Invalid input arguments, expected " + ", ".join(set(map(lambda x: str(x), args))) + "!")
11                    
12                    return func(*positional_args, **keyword_args)
13                else:
14                    result = func(*positional_args, **keyword_args)
15                    
16                    if type(result) not in args:
17                        print("Invalid output value, expected " + ", ".join(set(map(lambda x: str(x), args))) + "!")
18                    
19                    return result
20            return parameters
21        return modified_func
22    return inject_parameters

.E..
======================================================================
ERROR: test_check_decorated_exception (test.TestTypeCheck.test_check_decorated_exception)
The decorator should not supress any exceptions raised.
----------------------------------------------------------------------
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 111, in test_check_decorated_exception
decorated({1, 2, 3, 4, 5, 6})
File "/tmp/solution.py", line 14, in parameters
result = func(*positional_args, **keyword_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 6, in parameters
expected_parameters = set(filter(lambda x: (type(x) not in args), positional_args))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'set'

----------------------------------------------------------------------
Ran 4 tests in 0.021s

FAILED (errors=1)

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