Created
November 15, 2022 05:31
-
-
Save Shalabyelectronics/12163eaf96f5d1081c8f8f91a901d5c1 to your computer and use it in GitHub Desktop.
Decorator Check datatype example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_type(current_type:object): | |
""" | |
This Decorator will check the datatype of the | |
target function to validate the datatype arguments. | |
""" | |
def get_func(func): | |
def wrapper(*args,**kwargs): | |
check_values = [] | |
bad_values = [] | |
if args: | |
for arg in args: | |
if isinstance(arg,current_type): | |
check_values.append(True) | |
else: | |
check_values.append(False) | |
bad_values.append(arg) | |
else: | |
for key, value in kwargs.items(): | |
if isinstance(value,current_type): | |
check_values.append(True) | |
else: | |
check_values.append(False) | |
bad_values.append(value) | |
if all(check_values): | |
return func(*args,**kwargs) | |
if len(bad_values) == 1: | |
return f"Wrong Data Type: {bad_values[0]} is not {current_type} datatype." | |
return f"Wrong Data Type: {','.join(list(map(str,bad_values)))} are not {current_type} datatype." | |
return wrapper | |
return get_func | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment