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 合并文件的具体实例
Aug 08 Python
Python 自动补全(vim)
Nov 30 Python
Python中类的定义、继承及使用对象实例详解
Apr 30 Python
深入理解NumPy简明教程---数组2
Dec 17 Python
Python基于递归算法求最小公倍数和最大公约数示例
Jul 27 Python
Python UnboundLocalError和NameError错误根源案例解析
Oct 31 Python
Python3离线安装Requests模块问题
Oct 13 Python
Python scrapy增量爬取实例及实现过程解析
Dec 24 Python
Python vtk读取并显示dicom文件示例
Jan 13 Python
PyTorch的Debug指南
May 07 Python
OpenCV-Python实现轮廓的特征值
Jun 09 Python
Python GUI编程之tkinter 关于 ttkbootstrap 的使用详解
Mar 03 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
dede3.1分页文字采集过滤规则详说(图文教程)续四
2007/04/03 PHP
一个PHP的String类代码
2010/04/20 PHP
PHP设计模式之简单投诉页面实例
2016/02/24 PHP
颜色选择器 Color Picker,IE,Firefox,Opera,Safar
2010/11/25 Javascript
Extjs4 类的定义和扩展实例
2013/06/28 Javascript
jQuery学习笔记之 Ajax操作篇(一) - 数据加载
2014/06/23 Javascript
jQuery实现视频作为全屏幕背景
2014/12/18 Javascript
jquery左右全屏大尺寸多图滑动效果代码分享
2015/08/28 Javascript
javascript html5移动端轻松实现文件上传
2020/03/27 Javascript
使用PHP+JavaScript将HTML页面转换为图片的实例分享
2016/04/18 Javascript
如何解决IONIC页面底部被遮住无法向上滚动问题
2016/09/06 Javascript
jQuery UI仿淘宝搜索下拉列表功能
2017/01/10 Javascript
JavaScript表单验证的两种实现方法
2017/02/11 Javascript
vue.js框架实现表单排序和分页效果
2017/08/09 Javascript
微信小程序实现验证码获取倒计时效果
2018/02/08 Javascript
jQuery实现的简单获取索引功能示例
2018/06/04 jQuery
vue实现一个6个输入框的验证码输入组件功能的实例代码
2020/06/29 Javascript
python使用xlrd与xlwt对excel的读写和格式设定
2017/01/21 Python
Python管理Windows服务小脚本
2018/03/12 Python
python实现顺时针打印矩阵
2019/03/02 Python
python实现BP神经网络回归预测模型
2019/08/09 Python
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2019/11/19 Python
Python爬虫requests库多种用法实例
2020/05/28 Python
keras model.fit 解决validation_spilt=num 的问题
2020/06/19 Python
在keras里实现自定义上采样层
2020/06/28 Python
python中_del_还原数据的方法
2020/12/09 Python
HTML5 本地存储 LocalStorage详解
2016/06/24 HTML / CSS
智能电子秤、手表和健康监测仪:Withings(之前为诺基亚健康)
2018/10/30 全球购物
欧洲、亚洲、非洲和拉丁美洲的度假套餐:Great Value Vacations
2019/03/30 全球购物
医学毕业生自我鉴定
2013/10/30 职场文书
工程造价专业大学生职业规划范文
2014/03/09 职场文书
小学教师师德整改措施
2014/09/29 职场文书
观看焦裕禄观后感
2015/06/09 职场文书
国庆节新闻稿
2015/07/17 职场文书
Mysql实现主从配置和多主多从配置
2021/06/02 MySQL
MySQL聚簇索引和非聚簇索引的区别详情
2022/06/14 MySQL