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

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

1 точки общо

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

 1def type_check(stream):
 2    def types(*argument_types):
 3        def decorator(func):
 4            def wrapper(*args, **kwargs):
 5                if stream == "in":
 6                    arg_list = list(args) + list(kwargs.values())
 7                    for arg in arg_list:
 8                        if type(arg) not in argument_types:
 9                            print_types = ", ".join(map(str, argument_types))
10                            print(f"Invalid input arguments, expected {print_types}!")
11                            break
12                result = func(*args, **kwargs)
13                if stream == "out":
14                        if type(result) not in argument_types:
15                            print_types = ", ".join(map(str, argument_types))
16                            print(f"Invalid output arguments, expected {print_types}!")
17                return result
18            return wrapper
19        return decorator
20    return types
21
22@type_check("in")(str)
23def concatenate(*strings, separator=' beep boop '):
24    return separator.join(strings)
25
26print(concatenate(5, '6'))

Invalid input arguments, expected <class 'str'>!
sequence item 0: expected str instance, int found
File "/tmp/test_runner.py", line 75, in main
loaded_test = importlib.import_module('test', test_module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/tmp/test.py", line 5, in <module>
from solution import type_check
File "/tmp/solution.py", line 26, in <module>
print(concatenate(5, '6'))
^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 12, in wrapper
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 24, in concatenate
return separator.join(strings)
^^^^^^^^^^^^^^^^^^^^^^^

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