I have this function with the decorator validate_numeric
to validate if the input arguments of a function are numeric (types
int or float
).
If any of the arguments passed to the function are not numeric, it should return a message: The input arguments must be numeric
and stop execution. Otherwise, it should execute the decorated function normally.
def validate_numeric(func): # Decorator
def wrapper(*args, **kwargs):
if not isinstance(arg, (int, float)):
return "The input arguments must be numeric."
for key, value in kwargs.items():
if not isinstance(value, (int, float)):
return "The input arguments must be numeric."
return func(*args, **kwargs)
def sum(a, b): # Return the sum of two numbers
<code>
def validate_numeric(func): # Decorator
def wrapper(*args, **kwargs):
for arg in args:
if not isinstance(arg, (int, float)):
return "The input arguments must be numeric."
for key, value in kwargs.items():
if not isinstance(value, (int, float)):
return "The input arguments must be numeric."
return func(*args, **kwargs)
return wrapper
@validate_numeric
def sum(a, b): # Return the sum of two numbers
return a + b
print(sum(1, 2))
print(sum(1, "2"))
print(sum(a=1, b="a"))
print(sum(a=1, b=3.4))
</code>
def validate_numeric(func): # Decorator
def wrapper(*args, **kwargs):
for arg in args:
if not isinstance(arg, (int, float)):
return "The input arguments must be numeric."
for key, value in kwargs.items():
if not isinstance(value, (int, float)):
return "The input arguments must be numeric."
return func(*args, **kwargs)
return wrapper
@validate_numeric
def sum(a, b): # Return the sum of two numbers
return a + b
print(sum(1, 2))
print(sum(1, "2"))
print(sum(a=1, b="a"))
print(sum(a=1, b=3.4))
it is working fine, this is the output:
The input arguments must be numeric.
The input arguments must be numeric.
<code>3
The input arguments must be numeric.
The input arguments must be numeric.
4.4
</code>
3
The input arguments must be numeric.
The input arguments must be numeric.
4.4
now, i need Keep the previous code and create an additional decorator named debug
.
This will let us get some information on the execution of the function without having to add print all over the body of the function.
This decorator should simply output information about the input and output arguments. It should indicate both the positional and keyword arguments passed to the function and the output before returning it.
Use the following test cases:
"""Return the sum of two numbers."""
<code>@debug
def sum(a, b):
"""Return the sum of two numbers."""
return a + b
sum(1, 2)
sum(a=1, b=2)
sum(1, "a")
</code>
@debug
def sum(a, b):
"""Return the sum of two numbers."""
return a + b
sum(1, 2)
sum(a=1, b=2)
sum(1, "a")
Your result should look like this:
Positional arguments: 1,2
There are no keyword arguments
There are no positional arguments
Keyword arguments: a=1,b=2
Positional arguments: 1,a
There are no keyword arguments
Result: The input arguments must be numeric
<code>**********
Positional arguments: 1,2
There are no keyword arguments
Result: 3
**********
There are no positional arguments
Keyword arguments: a=1,b=2
Result: 3
**********
Positional arguments: 1,a
There are no keyword arguments
Result: The input arguments must be numeric
</code>
**********
Positional arguments: 1,2
There are no keyword arguments
Result: 3
**********
There are no positional arguments
Keyword arguments: a=1,b=2
Result: 3
**********
Positional arguments: 1,a
There are no keyword arguments
Result: The input arguments must be numeric
I try to create this
def wrapper(*args, **kwargs):
print(f"Function name: {func.__name__}")
print(f"Arguments: {args}")
print(f"Keyword arguments: {kwargs}")
return func(*args, **kwargs)
def validate_numeric(func):
def wrapper(*args, **kwargs):
if not isinstance(arg, (int, float)):
return "The input arguments must be numeric."
for key, value in kwargs.items():
if not isinstance(value, (int, float)):
return "The input arguments must be numeric."
return func(*args, **kwargs)
def sum(a, b): #Return the sum of two numbers
<code>def debug(func):
def wrapper(*args, **kwargs):
print(f"Function name: {func.__name__}")
print(f"Arguments: {args}")
print(f"Keyword arguments: {kwargs}")
return func(*args, **kwargs)
return wrapper
def validate_numeric(func):
def wrapper(*args, **kwargs):
for arg in args:
if not isinstance(arg, (int, float)):
return "The input arguments must be numeric."
for key, value in kwargs.items():
if not isinstance(value, (int, float)):
return "The input arguments must be numeric."
return func(*args, **kwargs)
return wrapper
@debug
def sum(a, b): #Return the sum of two numbers
return a + b
#1st case
print(sum(1, 2))
#2nd case
print(sum(a=1, b=2))
#3rd case
print(sum(1,"a"))
</code>
def debug(func):
def wrapper(*args, **kwargs):
print(f"Function name: {func.__name__}")
print(f"Arguments: {args}")
print(f"Keyword arguments: {kwargs}")
return func(*args, **kwargs)
return wrapper
def validate_numeric(func):
def wrapper(*args, **kwargs):
for arg in args:
if not isinstance(arg, (int, float)):
return "The input arguments must be numeric."
for key, value in kwargs.items():
if not isinstance(value, (int, float)):
return "The input arguments must be numeric."
return func(*args, **kwargs)
return wrapper
@debug
def sum(a, b): #Return the sum of two numbers
return a + b
#1st case
print(sum(1, 2))
#2nd case
print(sum(a=1, b=2))
#3rd case
print(sum(1,"a"))
but i got and error with the 3rd case, i got this error:
********************************************************************************
********************************************************************************
Keyword arguments: {'a': 1, 'b': 2}
********************************************************************************
Traceback (most recent call last):
File ".../task-2.py", line 40, in <module>
File ".../task-2.py", line 14, in wrapper
return func(*args, **kwargs)
File ".../task-2.py", line 32, in sum
TypeError: unsupported operand type(s) for +: 'int' and 'str'
<code>
Decorator @debug
********************************************************************************
Function name: sum
Arguments: (1, 2)
Keyword arguments: {}
3
********************************************************************************
Function name: sum
Arguments: ()
Keyword arguments: {'a': 1, 'b': 2}
3
********************************************************************************
Function name: sum
Arguments: (1, 'a')
Keyword arguments: {}
Traceback (most recent call last):
File ".../task-2.py", line 40, in <module>
print(sum(1,"a"))
File ".../task-2.py", line 14, in wrapper
return func(*args, **kwargs)
File ".../task-2.py", line 32, in sum
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
</code>
Decorator @debug
********************************************************************************
Function name: sum
Arguments: (1, 2)
Keyword arguments: {}
3
********************************************************************************
Function name: sum
Arguments: ()
Keyword arguments: {'a': 1, 'b': 2}
3
********************************************************************************
Function name: sum
Arguments: (1, 'a')
Keyword arguments: {}
Traceback (most recent call last):
File ".../task-2.py", line 40, in <module>
print(sum(1,"a"))
File ".../task-2.py", line 14, in wrapper
return func(*args, **kwargs)
File ".../task-2.py", line 32, in sum
return a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
The output must be like this:
Positional arguments: 1,2
There are no keyword arguments
There are no positional arguments
Keyword arguments: a=1,b=2
Positional arguments: 1,a
There are no keyword arguments
Result: The input arguments must be numeric
<code>
**********
Positional arguments: 1,2
There are no keyword arguments
Result: 3
**********
There are no positional arguments
Keyword arguments: a=1,b=2
Result: 3
**********
Positional arguments: 1,a
There are no keyword arguments
Result: The input arguments must be numeric
</code>
**********
Positional arguments: 1,2
There are no keyword arguments
Result: 3
**********
There are no positional arguments
Keyword arguments: a=1,b=2
Result: 3
**********
Positional arguments: 1,a
There are no keyword arguments
Result: The input arguments must be numeric