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

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

1 точки общо

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

 1def type_check(arg_type):
 2    def decorator(*types):
 3        # types = tuple(...) => slow lookup speed ~O(n)
 4        def inner(func):
 5            def wrapper(*args, **kwargs):
 6                # Check input types
 7                if arg_type == 'in':
 8                    expected_types_str = ', '.join([str(t) for t in types])
 9                    # Check args
10                    for arg in args:
11                        if arg not in types: # slow
12                            print(f'Invalid input arguments, expected {expected_types_str}!')
13                            break
14
15                    #Check kwargs
16                    for key, value in kwargs.items():
17                        if value not in types: # slow
18                            print(f'Invalid input arguments, expected {expected_types_str}!')
19                            break
20
21                result = func(*args, **kwargs)
22
23                # Check output type
24                if arg_type == 'out':
25                    expected_types_str = ', '.join([str(t) for t in types])
26                    if not isinstance(result, types):
27                        print(f'Invalid output value, expected {expected_types_str}!')
28
29                return result
30
31            return wrapper
32
33        return inner
34
35    return decorator

F.F.
======================================================================
FAIL: test_check_both (test.TestTypeCheck.test_check_both)
The decorator should report invalid "in" and "out" together.
----------------------------------------------------------------------
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 101, in test_check_both
self.assertEqual(mock_print.call_count, 2)
AssertionError: 3 != 2

======================================================================
FAIL: 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 61, in test_check_in
mock_print.assert_not_called()
File "/usr/lib/python3.12/unittest/mock.py", line 905, in assert_not_called
raise AssertionError(msg)
AssertionError: Expected 'print' to not have been called. Called 1 times.
Calls: [call("Invalid input arguments, expected <class 'list'>, <class 'tuple'>, <class 'set'>, <class 'generator'>!")].

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

FAILED (failures=2)

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