python中validators库的使用方法详解


Posted in Python onSeptember 23, 2022

前言

首先安装validators库:

pip install validators

python中validators库的使用方法详解

validators.between(value, min=None, max=None)

验证一个数字value是否在最小值min和最大值max之间,value不仅仅可以是整数,也可以是其它数据类型,例如floats, decimals 和 dates。

源码解析:

def between(value, min=None, max=None):
    """
    Validate that a number is between minimum and/or maximum value.
    This will work with any comparable type, such as floats, decimals and dates
    not just integers.
    This validator is originally based on `WTForms NumberRange validator`_.
    .. _WTForms NumberRange validator:
       https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
    Examples::
        >>> from datetime import datetime
        >>> between(5, min=2)
        True
        >>> between(13.2, min=13, max=14)
        True
        >>> between(500, max=400)
        ValidationFailure(func=between, args=...)
        >>> between(
        ...     datetime(2000, 11, 11),
        ...     min=datetime(1999, 11, 11)
        ... )
        True
    :param min:
        The minimum required value of the number. If not provided, minimum
        value will not be checked.
    :param max:
        The maximum value of the number. If not provided, maximum value
        will not be checked.
    .. versionadded:: 0.2
    """

示例代码:

from validators import between
from datetime import datetime
 
aa = between(3, min=2)
print(aa)
 
bb = between(3, min=2, max=5)
print(bb)
 
cc = between(3, max=2)
print(cc)
 
dd = between(datetime(2022, 9, 21), min=datetime(2022, 9, 19))
print(dd)
 
ee = between(datetime(2022, 9, 21), min=datetime(2022, 9, 19), max=datetime(2022, 10, 19))
print(ee)
 
ff = between(datetime(2022, 9, 21), max=datetime(2022, 9, 19))
print(ff)

运行结果:

python中validators库的使用方法详解

validators.domain(value)

验证value是否是一个有效域。如果value是一个有效域名,函数返回True, 否则返回 ValidationFailure.

源码解析:

def domain(value):
    """
    Return whether or not given value is a valid domain.
    If the value is valid domain name this function returns ``True``, otherwise
    :class:`~validators.utils.ValidationFailure`.
    Examples::
        >>> domain('example.com')
        True
        >>> domain('example.com/')
        ValidationFailure(func=domain, ...)
    Supports IDN domains as well::
        >>> domain('xn----gtbspbbmkef.xn--p1ai')
        True
    .. versionadded:: 0.9
    .. versionchanged:: 0.10
        Added support for internationalized domain name (IDN) validation.
    :param value: domain string to validate
    """

示例代码:

from validators import domain
 
aa = domain('example.com')
print(aa)
 
bb = domain('example..com')
print(bb)

运行结果:

python中validators库的使用方法详解

validators.email(value, whitelist=None)

验证是否是合法的邮件地址,如果是,函数返回True, 否则返回 ValidationFailure.

源码解析:

def email(value, whitelist=None):
    """
    Validate an email address.
    This validator is based on `Django's email validator`_. Returns
    ``True`` on success and :class:`~validators.utils.ValidationFailure`
    when validation fails.
    Examples::
        >>> email('someone@example.com')
        True
        >>> email('bogus@@')
        ValidationFailure(func=email, ...)
    .. _Django's email validator:
       https://github.com/django/django/blob/master/django/core/validators.py
    .. versionadded:: 0.1
    :param value: value to validate
    :param whitelist: domain names to whitelist
    :copyright: (c) Django Software Foundation and individual contributors.
    :license: BSD
    """

示例代码:

from validators import email
 
aa = email("123@qq.com")
print(aa)
 
bb = email("123@.com")
print(bb)

运行结果:

python中validators库的使用方法详解

validators.ip_address.ipv4(value) 

验证是否是合法的ipv4地址,如果是,函数返回True, 否则返回 ValidationFailure.

源码解析:

def ipv4(value):
    """
    Return whether a given value is a valid IP version 4 address.
    This validator is based on `WTForms IPAddress validator`_
    .. _WTForms IPAddress validator:
       https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
    Examples::
        >>> ipv4('123.0.0.7')
        True
        >>> ipv4('900.80.70.11')
        ValidationFailure(func=ipv4, args={'value': '900.80.70.11'})
    .. versionadded:: 0.2
    :param value: IP address string to validate
    """

示例代码:

from validators.ip_address import ipv4
 
aa = ipv4("127.0.0.1")
print(aa)
 
bb = ipv4("333.1.1.1")
print(bb)

运行结果:

python中validators库的使用方法详解

validators.ip_address.ipv6(value)

验证是否是合法的ipv6地址,如果是,函数返回True, 否则返回 ValidationFailure.

源码解析:

def ipv6(value):
    """
    Return whether a given value is a valid IP version 6 address
    (including IPv4-mapped IPv6 addresses).
    This validator is based on `WTForms IPAddress validator`_.
    .. _WTForms IPAddress validator:
       https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
    Examples::
        >>> ipv6('abcd:ef::42:1')
        True
        >>> ipv6('::ffff:192.0.2.128')
        True
        >>> ipv6('::192.0.2.128')
        True
        >>> ipv6('abc.0.0.1')
        ValidationFailure(func=ipv6, args={'value': 'abc.0.0.1'})
    .. versionadded:: 0.2
    :param value: IP address string to validate
    """

示例代码:

from validators.ip_address import ipv6
 
aa = ipv6("abcd:ef::42:1")
print(aa)
 
bb = ipv6("abc.0.0.1")
print(bb)

运行效果:

python中validators库的使用方法详解

validators.length(value, min=None, max=None) 

验证给定的字符串长度是否在指定范围内。

示例代码:

from validators import length
 
aa = length('aaa', min=2)
print(aa)
 
bb = length("aaa", min=2, max=5)
print(bb)
 
cc = length("aaa", max=2)
print(cc)

运行结果:

python中validators库的使用方法详解

validators.mac_address(value) 

验证是否是合法的mac地址,如果是,函数返回True, 否则返回 ValidationFailure.

示例代码:

from validators import mac_address
 
aa = mac_address('01:23:45:67:ab:CD')
print(aa)
 
bb = mac_address('00:00:00:00:00')
print(bb)

运行结果:

python中validators库的使用方法详解

validators.url(value, public=False) 

验证是否是合法的url,如果是,函数返回True, 否则返回 ValidationFailure.

Parameters:

  • value – 要验证的url
  • public – (default=False) Set True to only allow a public IP address

示例代码:

from validators import url
 
aa = url('http://www.baidu.com')
print(aa)
 
bb = url("www.baidu.com")
print(bb)
 
cc = url("http://127.0.0.1")
print(cc)
 
dd = url("http://127.0.0.1", public=True)
print(dd)

运行结果:

python中validators库的使用方法详解

到此这篇关于python中validators库的使用方法详解的文章就介绍到这了,更多相关python validators库内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python命令行参数sys.argv使用示例
Jan 28 Python
高性能web服务器框架Tornado简单实现restful接口及开发实例
Jul 16 Python
Python基础篇之初识Python必看攻略
Jun 23 Python
简单谈谈Python流程控制语句
Dec 04 Python
mac安装pytorch及系统的numpy更新方法
Jul 26 Python
opencv3/python 鼠标响应操作详解
Dec 11 Python
Pytorch to(device)用法
Jan 08 Python
详解用Python调用百度地图正/逆地理编码API
Jul 02 Python
Python下划线5种含义代码实例解析
Jul 10 Python
python在linux环境下安装skimage的示例代码
Oct 14 Python
Python爬取酷狗MP3音频的步骤
Feb 26 Python
python实战之90行代码写个猜数字游戏
Apr 22 Python
Python pyecharts案例超市4年数据可视化分析
Aug 14 #Python
Python编写车票订购系统 Python实现快递收费系统
Aug 14 #Python
Golang Web 框架Iris安装部署
Aug 14 #Python
python manim实现排序算法动画示例
Python 操作pdf pdfplumber读取PDF写入Exce
Aug 14 #Python
Python使用plt.boxplot()函数绘制箱图、常用方法以及含义详解
Aug 14 #Python
基于Python实现nc批量转tif格式
Aug 14 #Python
You might like
zf框架的session会话周期及次数限制使用示例
2014/03/13 PHP
深入理解PHP JSON数组与对象
2016/07/19 PHP
PHP中SERIALIZE和JSON的序列化与反序列化操作区别分析
2016/10/11 PHP
基于PHP的登录和注册的功能的实现
2020/08/06 PHP
用jscript实现新建word文档
2007/06/15 Javascript
jquery中的mouseleave和mouseout的区别 模仿下拉框效果
2012/02/07 Javascript
解决jquery异步按一定的时间间隔刷新问题
2012/12/10 Javascript
js获取视频时长代码
2014/04/10 Javascript
javascript控制在光标位置插入文字适合表情的插入
2014/06/09 Javascript
jQuery的css()方法用法实例
2014/12/24 Javascript
JS实现当前页居中分页效果的方法
2015/06/18 Javascript
AngularJS表单验证中级篇(3)
2016/09/28 Javascript
从零学习node.js之模块规范(一)
2017/02/21 Javascript
Vue.use源码分析
2017/04/22 Javascript
jQuery实现手机号正则验证输入及自动填充空格功能
2018/01/02 jQuery
富文本编辑器vue2-editor实现全屏功能
2019/05/26 Javascript
微信小程序全局变量改变监听的实现方法
2019/07/15 Javascript
vue实现路由监听和参数监听
2019/10/29 Javascript
[07:57]DOTA2热力大趴狂欢夜 广州站活动回顾
2013/11/27 DOTA
[54:10]完美世界DOTA2联赛PWL S2 Magma vs FTD 第二场 11.29
2020/12/03 DOTA
Python 循环语句之 while,for语句详解
2018/04/23 Python
python实现泊松图像融合
2018/07/26 Python
python实现简单日期工具类
2019/04/24 Python
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
2019/08/21 Python
使用CSS3的背景渐变Text Gradient 创建文字颜色渐变
2014/08/19 HTML / CSS
html5 利用canvas实现超级玛丽简单动画
2013/09/06 HTML / CSS
保险专业自荐信范文
2014/02/20 职场文书
社区平安建设汇报材料
2014/08/14 职场文书
2014党员学习兰辉先进事迹思想汇报
2014/09/17 职场文书
2014年设计师工作总结
2014/11/25 职场文书
2014年留守儿童工作总结
2014/12/10 职场文书
2016十一国庆节感言
2015/12/09 职场文书
小学班级标语口号大全
2015/12/26 职场文书
python实战之90行代码写个猜数字游戏
2021/04/22 Python
Golang的继承模拟实例
2021/06/30 Golang
springboot创建的web项目整合Quartz框架的项目实践
2022/06/21 Java/Android