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

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

1 точки общо

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

 1""" Verify the input and output data types of a function. """
 2
 3
 4def type_check(io_command):
 5    """ Check if the command is in- or output."""
 6    def type_requirement(*types):
 7        """ Check if the input data matches the required types """
 8        def decorator(func):
 9            def operation(*args, **kwargs):
10                expected_types = ", ".join(str(every_type) for every_type in types)
11                if io_command == "in":
12                    for arg in args:
13                        if not any(isinstance(arg, every_type) for every_type in types):
14                            print(f"Invalid input arguments, expected {expected_types}!")
15
16                result = func(*args, **kwargs)
17
18                if io_command == "out" and not any(isinstance(result, every_type) for every_type in types):
19                    print(f"Invalid output value, expected {expected_types}!")
20
21                return result
22            return operation
23        return decorator
24    return type_requirement

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 43, in test_check_in
self.assertEqual(mock_print.call_count, 1)
AssertionError: 2 != 1

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

FAILED (failures=2)

Дискусия
История

f1""" Verify the input and output data types of a function. """f1""" Verify the input and output data types of a function. """
22
33
4def type_check(io_command):4def type_check(io_command):
5    """ Check if the command is in- or output."""5    """ Check if the command is in- or output."""
6    def type_requirement(*types):6    def type_requirement(*types):
7        """ Check if the input data matches the required types """7        """ Check if the input data matches the required types """
8        def decorator(func):8        def decorator(func):
9            def operation(*args, **kwargs):9            def operation(*args, **kwargs):
10                expected_types = ", ".join(str(every_type) for every_type in types)10                expected_types = ", ".join(str(every_type) for every_type in types)
11                if io_command == "in":11                if io_command == "in":
12                    for arg in args:12                    for arg in args:
13                        if not any(isinstance(arg, every_type) for every_type in types):13                        if not any(isinstance(arg, every_type) for every_type in types):
14                            print(f"Invalid input arguments, expected {expected_types}!")14                            print(f"Invalid input arguments, expected {expected_types}!")
1515
16                result = func(*args, **kwargs)16                result = func(*args, **kwargs)
1717
18                if io_command == "out" and not any(isinstance(result, every_type) for every_type in types):18                if io_command == "out" and not any(isinstance(result, every_type) for every_type in types):
t19                    print(f"Invalid output arguments, expected {expected_types}!")t19                    print(f"Invalid output value, expected {expected_types}!")
2020
21                return result21                return result
22            return operation22            return operation
23        return decorator23        return decorator
24    return type_requirement24    return type_requirement
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1""" Verify the input and output data types of a function. """f1""" Verify the input and output data types of a function. """
22
33
4def type_check(io_command):4def type_check(io_command):
5    """ Check if the command is in- or output."""5    """ Check if the command is in- or output."""
6    def type_requirement(*types):6    def type_requirement(*types):
7        """ Check if the input data matches the required types """7        """ Check if the input data matches the required types """
8        def decorator(func):8        def decorator(func):
9            def operation(*args, **kwargs):9            def operation(*args, **kwargs):
10                expected_types = ", ".join(str(every_type) for every_type in types)10                expected_types = ", ".join(str(every_type) for every_type in types)
11                if io_command == "in":11                if io_command == "in":
12                    for arg in args:12                    for arg in args:
13                        if not any(isinstance(arg, every_type) for every_type in types):13                        if not any(isinstance(arg, every_type) for every_type in types):
14                            print(f"Invalid input arguments, expected {expected_types}!")14                            print(f"Invalid input arguments, expected {expected_types}!")
1515
16                result = func(*args, **kwargs)16                result = func(*args, **kwargs)
1717
18                if io_command == "out" and not any(isinstance(result, every_type) for every_type in types):18                if io_command == "out" and not any(isinstance(result, every_type) for every_type in types):
t19                    print(f"Invalid output value, expected {expected_types}!")t19                    print(f"Invalid output arguments, expected {expected_types}!")
2020
21                return result21                return result
22            return operation22            return operation
23        return decorator23        return decorator
24    return type_requirement24    return type_requirement
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op