decorator for fast check types of arguments which are given to function
Project description
Overview.
char stands for: check of arguments.
This library gives to user ability to check types of function arguments via one decorator.
P.S. I like to use variables notation because it really improves readability of the code.
Example
from char import char
# OR from char import check_types_of_arguments # They are equivalent
@char
def func(int_x):
pass
ArgumentTypeError: Incorrect type of variable was given to function: func
---> For variable: int_x
---> Were given value: pewpew
---> With type: <class 'str'>
---> Instead of: <class 'int'>
Differences from python type hinting
Additional difference is that this package supports python >=2.7 when type hinting is available only since python 3.5
In case if you don’t want to use any notation of variables and want to use the type hinting then you can try the library typeguard.
Installation via pip:
pip install char
Usage with default settings
Default prefixes
Here will be a list of name prefixes and which type the variable is expected to be
If variable name doesn’t start with ony of the given prefixes then variable type won’t be checked.
“any_” - object
“bool_” - bool
“b_” - bool
“is_” - bool
“has_” - bool
“str_” - str
“bytes_” - bytes
“int_” - int
“i_” - int
“float_” - float
“f_” - float
“list_” - list
“l_” - list
“dict_” - dict
“d_” - dict
“set_” - set
“tuple_” - tuple
“t_” - tuple
Example
from char import char
@char
def oh_my_god(
int_arg,
float_arg,
list_arg,
undef_arg,
d_kwarg=None,
i_kwarg=0,
is_kwarg=False
):
pass
oh_my_god(0, 0.0, [], 1) # Will PASS
oh_my_god(0, 0.0, None, "text") # Will PASS
oh_my_god(0, 0.0, {}, "text") # Will FAIL and raise an ArgumentTypeError
oh_my_god(0, 0.0, [], Exception, d_kwarg={0: 1}) # Will PASS
oh_my_god(0, 0.0, [], object, is_kwarg=0) # Will FAIL and raise an ArgumentTypeError
Usage with user defined settings
Decorator arguments
bool_is_to_skip_None_value=True: Flag what to do with None values, by default None values won’t be checked.
dict_tuple_types_by_prefix_to_update_default: dictionary, which prefixes to add to the default ones
dict_tuple_types_by_prefix: dictionary, which prefixes to use instead of default ones
Decorator argument: bool_is_to_skip_None_value
@char
def func_with_default_decorator(dict_x):
pass
@char(bool_is_to_skip_None_value=False)
def func_with_custom_decorator(dict_x):
pass
func_with_default_decorator(None) # Will PASS
func_with_custom_decorator(None) # Will FAIL and raise an ArgumentTypeError
Decorator argument: dict_tuple_types_by_prefix_to_update_default
@char(dict_tuple_types_by_prefix_to_update_default={"num_": (int, float, bool)})
def very_complex_function(num_x, str_y=""):
pass
very_complex_function(0, "hihi") # Will PASS
very_complex_function(0.5, "heyhey") # Will PASS
very_complex_function(True) # Will PASS
very_complex_function("True") # Will FAIL and raise an ArgumentTypeError
Decorator argument: dict_tuple_types_by_prefix
@char(dict_tuple_types_by_prefix={"exception": (BaseException)})
def function_with_only_one_check(int_x, exception_y=None):
pass
function_with_only_one_check(0, Exception) # Will PASS
function_with_only_one_check(0.5, TypeError) # Will PASS because first variable won't be checked
function_with_only_one_check(0.5, "ERROR") # Will FAIL and raise an ArgumentTypeError
Links
Project local Links
Contacts
License
This project is licensed under the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file char-0.1.2.tar.gz
.
File metadata
- Download URL: char-0.1.2.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6020cf4c81e43484e0b0a1d5d0668a5e56dfff4dadea24629d92a2a3934c79a |
|
MD5 | 603afee26ac1d22cb2f0972a238bb255 |
|
BLAKE2b-256 | 66a15499c63ba905cd7619de28969a5c9b642addbbda56551b52b47d24e76272 |
File details
Details for the file char-0.1.2-py2.py3-none-any.whl
.
File metadata
- Download URL: char-0.1.2-py2.py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2719f713a62134657f51da07ebc6e5c2acb11862f65b8be14e6b596fa345e80 |
|
MD5 | 3f7db467e1af9eba35876b4044855136 |
|
BLAKE2b-256 | aeac676c193bb5158bfedfe485ed9c27ebcea8bf44c21529cbdf7a7cb4f831c0 |