公司网站开发可行性报告,WordPress443端口免备案,银行营销活动方案,wordpress+采集评论pyparamvalidate 是一个简单易用的函数参数验证器。它提供了各种内置验证器#xff0c;支持自定义验证规则#xff0c;有助于 python 开发人员轻松进行函数参数验证#xff0c;提高代码的健壮性和可维护性。
项目地址#xff1a;github
安装
pip install pyparamvalidat…pyparamvalidate 是一个简单易用的函数参数验证器。它提供了各种内置验证器支持自定义验证规则有助于 python 开发人员轻松进行函数参数验证提高代码的健壮性和可维护性。
项目地址github
安装
pip install pyparamvalidate如果安装过程中提示 Failed to build numpy 错误 Failed to build numpy ERROR: Could not build wheels for numpy, which is required to install pyproject.toml-based projects 请先手动安装 numpy 库:
pip install numpy使用示例
示例 1无规则描述
from pyparamvalidate import ParameterValidator, ParameterValidationErrorParameterValidator(name).is_string().is_not_empty()
ParameterValidator(age).is_int().is_positive()
ParameterValidator(gender).is_allowed_value([male, female])
ParameterValidator(description).is_string().is_not_empty()
def example_function(name, age, gendermale, **kwargs):description kwargs.get(description)return name, age, gender, descriptionresult example_function(nameJohn, age25, gendermale, descriptionA person)
print(result) # output: (John, 25, male, A person)try:example_function(name123, age25, gendermale, descriptionA person)
except ParameterValidationError as e:print(e) # output: Parameter name in function example_function is invalid.示例 2在 ParameterValidator 实例化中描述规则
from pyparamvalidate import ParameterValidator, ParameterValidationErrorParameterValidator(name, param_rule_descriptionName must be a string).is_string().is_not_empty()
ParameterValidator(age, param_rule_descriptionAge must be a positive integer).is_int().is_positive()
ParameterValidator(gender, param_rule_descriptionGender must be either male or female).is_allowed_value([male, female])
ParameterValidator(description, param_rule_descriptionDescription must be a string).is_string().is_not_empty()
def example_function(name, age, gendermale, **kwargs):description kwargs.get(description)return name, age, gender, descriptionresult example_function(nameJohn, age25, gendermale, descriptionA person)
print(result) # output: (John, 25, male, A person)try:example_function(name123, age25, gendermale, descriptionA person)
except ParameterValidationError as e:print(e) # output: Parameter name in function example_function is invalid. Please refer to: Name must be a string示例 3在 验证器 中描述规则
from pyparamvalidate import ParameterValidator, ParameterValidationErrorParameterValidator(name).is_string(Name must be a string).is_not_empty(Name cannot be empty)
ParameterValidator(age).is_int(Age must be an integer).is_positive(Age must be a positive number)
ParameterValidator(gender).is_allowed_value([male, female], Gender must be either male or female)
ParameterValidator(description).is_string(Description must be a string).is_not_empty(Description cannot be empty)
def example_function(name, age, gendermale, **kwargs):description kwargs.get(description)return name, age, gender, descriptionresult example_function(nameJohn, age25, gendermale, descriptionA person)
print(result) # output: (John, 25, male, A person)try:example_function(name123, age25, gendermale, descriptionA person)
except ParameterValidationError as e:print(e) # Parameter name in function example_function is invalid. Error: Name must be a string可用的验证器
is_string检查参数是否为字符串。is_int检查参数是否为整数。is_positive检查参数是否为正数。is_float检查参数是否为浮点数。is_list检查参数是否为列表。is_dict检查参数是否为字典。is_set检查参数是否为集合。is_tuple检查参数是否为元组。is_not_none检查参数是否不为None。is_not_empty检查参数是否不为空对于字符串、列表、字典、集合等。is_allowed_value检查参数是否在指定的允许值范围内。max_length检查参数的长度是否不超过指定的最大值。min_length检查参数的长度是否不小于指定的最小值。is_substring检查参数是否为指定字符串的子串。is_subset检查参数是否为指定集合的子集。is_sublist检查参数是否为指定列表的子列表。contains_substring检查参数是否包含指定字符串。contains_subset检查参数是否包含指定集合。contains_sublist检查参数是否包含指定列表。is_file检查参数是否为有效的文件is_dir检查参数是否为有效的目录is_file_suffix检查参数是否以指定文件后缀结尾。is_similar_dict检查参数是否与指定字典相似如果key值相同value类型相同则判定为True支持比对嵌套字典。is_method检查参数是否为可调用的方法函数。
除了以上内置验证器外还可以使用 custom_validator 方法添加自定义验证器。
自定义验证器
from pyparamvalidate import ParameterValidatordef custom_check(value):return value % 2 0ParameterValidator(param).custom_validator(custom_check, Value must be an even number)
def example_function(param):return param更多使用方法
from pyparamvalidate.tests import test_param_validatortest_param_validator 是 ParameterValidator 的测试文件可点击 test_param_validator 参考更多使用方法。