高斯金字塔 拉普拉斯金字塔_金字塔学入门指南
高斯金字塔 拉普拉斯金字塔
The topic for today is on data validation and settings management using Python type hinting. We are going to use a Python package called pydantic
which enforces type hints at runtime. It provides user-friendly errors, allowing you to catch any invalid data. Based on the official documentation, Pydantic is
今天的主题是有关使用Python类型提示的数据验证和设置管理。 我们将使用一个名为pydantic
的Python程序包,该程序包在运行时会强制执行类型提示。 它提供了用户友好的错误,使您可以捕获任何无效数据。 根据官方文件,Pydantic是
“… primarily a parsing library, not a validation library. Validation is a means to an end: building a model which conforms to the types and constraints provided.
“…主要是解析库,而不是验证库。 验证是达到目的的一种手段:建立一个符合提供的类型和约束的模型。
In other words, pydantic guarantees the types and constraints of the output model, not the input data.”
换句话说,Pydantic保证了输出模型的类型和约束,而不是输入数据。”
There are three sections in this tutorial:
本教程分为三个部分:
- Setup建立
- Implementation实作
- Conclusion结论
Let’s proceed to the next section and start installing the necessary modules.
让我们继续下一节并开始安装必要的模块。
1.设定 (1. Setup)
It is highly recommended to create a virtual environment before you proceed with the installation.
强烈建议您在继续安装之前创建一个虚拟环境。
基本安装 (Basic installation)
Open up a terminal and run the following command to install pydantic
打开终端并运行以下命令以安装pydantic
pip install pydantic
升级现有软件包 (Upgrade existing package)
If you already have an existing package and would like to upgrade it, kindly run the following command:
如果您已经有一个现有软件包并想对其进行升级,请运行以下命令:
pip install -U pydantic
水蟒 (Anaconda)
For Anaconda users, you can install it as follows:
对于Anaconda用户,可以按以下方式安装:
conda install pydantic -c conda-forge
可选依赖项 (Optional dependencies)
pydantic
comes with the following optional dependencies based on your needs:
pydantic
根据您的需求附带以下可选依赖项:
email-validator
— Support for email validation.email-validator
支持电子邮件验证。typing-extensions
— Support use ofLiteral
prior to Python 3.8.typing-extensions
—支持在Python 3.8之前使用Literal
。python-dotenv
— Support fordotenv
file with settings.python-dotenv
—支持带有设置的dotenv
文件。
You can install them manually:
您可以手动安装它们:
# install email-validatorpip install email-validator# install typing-extensionspip install typing_extensions# install python-dotenvpip install python-dotenv
or along with pydantic
as follows:
或与pydantic
一起使用,如下所示:
# install email-validatorpip install pydantic[email]# install typing-extensionspip install pydantic[typing_extensions]# install python-dotenvpip install pydantic[dotenv]# install all dependenciespip install pydantic[email,typing_extensions,dotenv]
2.实施 (2. Implementation)
In this section, we are going to explore some of the useful functionalities available in pydantic
.
在本节中,我们将探索pydantic
可用的一些有用功能。
Defining an object in pydantic
is as simple as creating a new class which inherits from theBaseModel
. When you create a new object from the class, pydantic
guarantees that the fields of the resultant model instance will conform to the field types defined on the model.
在pydantic
定义对象就像创建一个继承自BaseModel
的新类一样简单。 当您从类中创建新对象时, pydantic
确保生成的模型实例的字段将与模型上定义的字段类型一致。
进口 (Import)
Add the following import declaration at the top of your Python file.
在Python文件顶部添加以下导入声明。
from datetime import datetimefrom typing import List, Optionalfrom pydantic import BaseModel
用户类别 (User class)
Declare a new class which inherits the BaseModel
as follow:
声明一个继承了BaseModel
的新类,如下所示:
class User(BaseModel): id: int username : str password : str confirm_password : str alias = 'anonymous' timestamp: Optional[datetime] = None friends: List[int] = []
pydantic
uses the built-in type hinting syntax to determine the data type of each variable. Let’s explore one by one what happens behind the scenes.
pydantic
使用内置的类型提示语法来确定每个变量的数据类型。 让我们一一探讨幕后发生的事情。
id
— An integer variable represents an ID. Since the default value is not provided, this field is required and must be specified during object creation. Strings, bytes, or floats will be coerced to integer if possible; otherwise, an exception will be raised.id
—一个整数变量代表一个ID。 由于未提供默认值,因此此字段是必需的,并且必须在对象创建期间指定。 如果可能,字符串,字节或浮点数将被强制为整数; 否则,将引发异常。username
— A string variable represents a username and is required.username
—一个字符串变量代表一个用户名,是必需的。password
— A string variable represents a password and is required.password
—字符串变量代表密码,是必需的。confirm_password
— A string variable represents a confirmation password and is required. It will be used for data validation later on.confirm_password
—字符串变量代表确认密码,是必需的。 稍后将用于数据验证。alias
— A string variable represents an alias. It is not required and will be set to anonymous if it is not provided during object creation.alias
—字符串变量表示别名。 它不是必需的,如果在对象创建期间未提供,它将设置为匿名。timestamp
— A date/time field, which is not required. Default to None.pydantic
will process either a unix timestamp int or a string representing the date/time.timestamp
—日期/时间字段,不是必需的。 默认为无。pydantic
将处理unix时间戳int或代表日期/时间的字符串。friends
— A list of integer inputs.friends
—整数输入的列表。
对象实例化 (Object instantiation)
The next step is to instantiate a new object from the User
class.
下一步是从User
类实例化一个新对象。
data = {'id': '1234', 'username': 'wai foong', 'password': 'Password123', 'confirm_password': 'Password123', 'timestamp': '2020-08-03 10:30', 'friends': [1, '2', b'3']}user = User(**data)
You should get the following output when you print out the user
variable. You can notice that id
has been automatically converted to an integer, even though the input is a string. Likewise, bytes are automatically converted to integers, as shown by the friends
field.
打印出user
变量时,应该获得以下输出。 您会注意到,即使输入是字符串, id
也已自动转换为整数。 同样,字节会自动转换为整数,如friends
字段所示。
id=1234 username='wai foong' password='Password123' confirm_password='Password123' timestamp=datetime.datetime(2020, 8, 3, 10, 30) friends=[1, 2, 3] alias='anonymous'
BaseModel下的方法和属性 (Methods and attributes under BaseModel)
Classes that inherit the BaseModel
will have the following methods and attributes:
继承BaseModel
类将具有以下方法和属性:
dict()
— returns a dictionary of the model’s fields and valuesdict()
—返回模型字段和值的字典json()
— returns a JSON string representation dictionaryjson()
—返回一个JSON字符串表示字典copy()
— returns a deep copy of the modelcopy()
—返回模型的深层副本parse_obj()
— a utility for loading any object into a model with error handling if the object is not a dictionaryparse_obj()
—如果对象不是字典,则用于通过错误处理将任何对象加载到模型中的实用程序parse_raw()
— a utility for loading strings of numerous formatsparse_raw()
—用于加载多种格式的字符串的实用程序parse_field()
— similar toparse_raw()
but meant for filesparse_field()
-类似于parse_raw()
但意味着文件from_orm()
— loads data into a model from an arbitrary classfrom_orm()
—将数据从任意类加载到模型中schema()
— returns a dictionary representing the model as JSON schemaschema()
—返回一个将模型表示为JSON模式的字典schema_json()
— returns a JSON string representation ofschema()
schema_json()
—返回schema()
的JSON字符串表示形式construct()
— a class method for creating models without running validationconstruct()
—一种无需运行验证即可创建模型的类方法__fields_set__
— Set of names of fields which were set when the model instance was initialized__fields_set__
—初始化模型实例时设置的字段名称集__fields__
— a dictionary of the model’s fields__fields__
—模型字段的字典__config__
— the configuration class for the model__config__
—模型的配置类
Let’s change the input for id
to a string as follows:
让我们将id
的输入更改为字符串,如下所示:
data = {'id': 'a random string', 'username': 'wai foong', 'password': 'Password123', 'confirm_password': 'Password123', 'timestamp': '2020-08-03 10:30', 'friends': [1, '2', b'3']}user = User(**data)
You should get the following error when you run the code.
运行代码时,您应该得到以下错误。
value is not a valid integer (type=type_error.integer)
验证错误 (ValidationError)
In order to get better details on the error, it is highly recommended to wrap it inside a try-catch block, as follows:
为了获得有关错误的更好的详细信息,强烈建议将其包装在try-catch块中,如下所示:
from pydantic import BaseModel, ValidationError# ... codes for User classdata = {'id': 'a random string', 'username': 'wai foong', 'password': 'Password123', 'confirm_password': 'Password123', 'timestamp': '2020-08-03 10:30', 'friends': [1, '2', b'3']}try: user = User(**data)except ValidationError as e: print(e.json())
It will print out the following JSON, which indicates that the input for id
is not a valid integer.
它将输出以下JSON,它表示id
的输入不是有效的整数。
[ { "loc": [ "id" ], "msg": "value is not a valid integer", "type": "type_error.integer" }]
栏位类型 (Field types)
pydantic
provides support for most of the common types from the Python standard library. The full list is as follows:
pydantic
为Python标准库中的大多数常见类型提供支持。 完整列表如下:
- bool布尔
- int整型
- float浮动
- str力量
- bytes个字节
- list清单
- tuple元组
- dict字典
- set组
- frozenset冰封
- datetime.datedatetime.date
- datetime.timedatetime.time
- datetime.datetimedatetime.datetime
- datetime.timedeltadatetime.timedelta
- typing.Any打字
- typing.TypeVarType.TypeVar
- typing.Union打字联盟
- typing.Optional键入。可选
- typing.List打字。清单
- typing.Tuple键入。元组
- typing.Dict打字。字典
- typing.Set打字
- typing.FrozenSet键入.FrozenSet
- typing.Sequence打字顺序
- typing.Iterable打字
- typing.Type类型
- typing.Callable打字
- typing.Pattern打字模式
- ipaddress.IPv4Addressipaddress.IPv4地址
- ipaddress.IPv4Interfaceipaddress.IPv4接口
- ipaddress.IPv4Networkipaddress.IPv4网络
- ipaddress.IPv6Addressipaddress.IPv6地址
- ipaddress.IPv6Interfaceipaddress.IPv6接口
- ipaddress.IPv6Networkipaddress.IPv6网络
- enum.Enum枚举
- enum.IntEnum枚举
- decimal.Decimal十进制。十进制
- pathlib.Path路径库
- uuid.UUIDuuid.UUID
- ByteSize字节大小
约束类型 (Constrained types)
You can enforce your own restriction via the Constrained Types
. Let’s have a look at the following example:
您可以通过Constrained Types
实施自己的限制。 让我们看下面的例子:
from pydantic import ( BaseModel, NegativeInt, PositiveInt, conint, conlist, constr)class Model(BaseModel): # minimum length of 2 and maximum length of 10 short_str: constr(min_length=2, max_length=10) # regex regex_str: constr(regex=r'^apple (pie|tart|sandwich)$') # remove whitespace from string strip_str: constr(strip_whitespace=True) # value must be greater than 1000 and less than 1024 big_int: conint(gt=1000, lt=1024)
# value is multiple of 5 mod_int: conint(multiple_of=5)
# must be a positive integer pos_int: PositiveInt
# must be a negative integer neg_int: NegativeInt # list of integers that contains 1 to 4 items short_list: conlist(int, min_items=1, max_items=4)
严格类型 (Strict types)
If you are looking for rigid restrictions which pass validation if and only if the validated value is of the respective type or is a subtype of that type, you can use the following strict types:
如果您正在寻找仅在经过验证的值属于相应类型或该类型的子类型时才通过验证的严格限制,则可以使用以下严格类型:
- StrictStr严格的
- StrictInt严格的
- StrictFloat严格浮动
- StrictBool严格布尔
The following example illustrates the proper way to enforce StrictBool
in your inherited class.
以下示例说明了在继承的类中强制执行StrictBool
的正确方法。
from pydantic import BaseModel, StrictBool,class StrictBoolModel(BaseModel): strict_bool: StrictBool
The string ‘False’
will raise ValidationError as it will only accept either True
or False
as input.
字符串'False'
将引发ValidationError,因为它仅接受True
或False
作为输入。
验证器 (Validator)
Furthermore, you can create your own custom validators using the validator
decorator inside your inherited class. Let’s have a look at the following example which determine if the id
is of four digits and whether the confirm_password
matches the password
field.
此外,您可以使用继承的类中的validator
装饰器来创建自己的自定义验证validator
。 让我们看下面的示例,该示例确定id
是否为四位数,以及confirm_password
是否与password
字段匹配。
from datetime import datetimefrom typing import List, Optionalfrom pydantic import BaseModel, ValidationError, validatorclass User(BaseModel): id: int username : str password : str confirm_password : str alias = 'anonymous' timestamp: Optional[datetime] = None friends: List[int] = [] @validator('id') def id_must_be_4_digits(cls, v): if len(str(v)) != 4: raise ValueError('must be 4 digits') return v @validator('confirm_password') def passwords_match(cls, v, values, **kwargs): if 'password' in values and v != values['password']: raise ValueError('passwords do not match') return v
3.结论 (3. Conclusion)
Let’s recap what we have learned today.
让我们回顾一下我们今天学到的东西。
We started off with a detailed explanation on Pydantic which helps to parse and validate data.
我们从有关Pydantic的详细说明开始,该说明有助于解析和验证数据。
Next, we created a virtual environment and installed Pydantic via pip or conda. It also includes support for three additional dependencies based on our use cases.
接下来,我们创建了一个虚拟环境,并通过pip或conda安装了Pydantic。 它还包括根据我们的用例支持的三个附加依赖项。
Once we were done with the installation, we explored in-depth the basic functionalities provided by the package. The basic building block is to create a new class which inherits from BaseModel
.
完成安装后,我们将深入探讨该软件包提供的基本功能。 基本构建块是创建一个继承自BaseModel
的新类。
We learned that Pydantic provides support for most of the common data types under Python standard library. We tested out both the Constrained Types
and Strict Types
which helps to enforce our own custom restrictions.
我们了解到Pydantic在Python标准库下提供了对大多数常见数据类型的支持。 我们测试了Constrained Types
和Strict Types
,这有助于实施我们自己的自定义限制。
Lastly, you played around with the validator
decorator to allow only four digits input for id
, and the confirm_password
must match the password
field.
最后,您与validator
修饰器一起使用,仅允许输入4位数字作为id
, confirm_password
必须与password
字段匹配。
Thanks for reading this piece. Hope to see you again in the next article!
感谢您阅读本文。 希望在下一篇文章中再见!
翻译自: https://medium.com/better-programming/the-beginners-guide-to-pydantic-ba33b26cde89
高斯金字塔 拉普拉斯金字塔
http://www.taodudu.cc/news/show-995025.html
相关文章:
- 语言认知偏差_我们的认知偏差正在破坏患者的结果数据
- python中定义数据结构_Python中的数据结构。
- plotly django_使用Plotly为Django HTML页面进行漂亮的可视化
- 软件工程方法学要素含义_日期时间数据的要素工程
- 数据湖 data lake_在Data Lake中高效更新TB级数据的模式
- ai对话机器人实现方案_显然地引入了AI —无代码机器学习解决方案
- 图片中的暖色或冷色滤色片是否会带来更多点击? —机器学习A / B测试
- 图卷积 节点分类_在节点分类任务上训练图卷积网络
- 回归分析预测_使用回归分析预测心脏病。
- aws spark_使用Spark构建AWS数据湖时的一些问题以及如何处理这些问题
- 数据科学家编程能力需要多好_我们不需要这么多的数据科学家
- sql优化技巧_使用这些查询优化技巧成为SQL向导
- 物种分布模型_减少物种分布建模中的空间自相关
- 清洁数据ploy n_清洁屋数据
- 基于边缘计算的实时绩效_基于绩效的营销中的三大错误
- 上凸包和下凸包_使用凸包聚类
- 决策树有框架吗_决策框架
- mysql那本书适合初学者_3本书适合初学者
- 阎焱多少身价_2020年,数据科学家的身价是多少?
- 卡尔曼滤波滤波方程_了解卡尔曼滤波器及其方程
- 朴素贝叶斯分类器 文本分类_构建灾难响应的文本分类器
- Seaborn:Python
- 销货清单数据_2020年8月数据科学阅读清单
- 米其林餐厅 盐之花_在世界范围内探索《米其林指南》
- spotify 数据分析_我的Spotify流历史分析
- 纹个鸡儿天才小熊猫_给熊猫用户的5个提示
- 图像离群值_什么是离群值?
- 数据预处理工具_数据预处理
- 自考数据结构和数据结构导论_我跳过大学自学数据科学
- 在PyTorch中转换数据
高斯金字塔 拉普拉斯金字塔_金字塔学入门指南相关推荐
- 数据分析从头学_数据新闻学入门指南:让我们从头开始构建故事
数据分析从头学 by Mina Demian 由Mina Demian 数据新闻学入门指南:让我们从头开始构建故事 (A Beginner's Guide to Data Journalism: Le ...
- app 隐私 自我评估指南_在线隐私入门指南
app 隐私 自我评估指南 by Iulian Gulea 由Iulian Gulea 在线隐私入门指南 (The Beginner's Guide To Online Privacy) We are ...
- 音频剪切_音频编辑入门指南:剪切,修剪和排列
音频剪切 Audacity novices often start with lofty project ideas, but sometimes they lack the basics. Know ...
- 不同语言编程能整合到一起吗_学习编程入门指南
每天都看到很多对编程感兴趣的人在问是不是可以自学软件开发,或者应该怎么自学编程才能入门.在这篇文章里,我将尝试重现一个初学者在学习计算机编程时可能会碰到的问题,并尽量提供相应的解决思路,希望对初学者有 ...
- 数位dp模板 最高位最低位_无纸化办公入门指南(数位板篇)
居家办公的这段时间,你们有没有遇到无纸化办公的问题? 作为一名编辑,在我社规定不能寄纸稿的情况下,看电子稿成了常态.如果是word版尚且可以批注,不影响工作效率.但如果是PDF呢?怎样保证改稿的效率? ...
- 硅麦克风的声学设计指南_电脑麦克风入门指南
硅麦克风的声学设计指南 With Gmail adding the ability to make free calls within the US and Canada, now is a grea ...
- 服务器和交换机物理连接_网络交换机入门指南
网络交换机的知识点非常多,我们较为简单的方式梳理一下,如果题主对某一个知识点感兴趣,可以进一步了解. 一.网络交换机的定义 这边我们将以通俗的方式介绍一下,随着网络上大量用户和程序以及服务器的出现,导 ...
- 简明易懂的c#入门指南_统计假设检验的简明指南
简明易懂的c#入门指南 介绍 (Introduction) One of the main applications of frequentist statistics is the comparis ...
- 读书笔记|《金字塔原理》_第一章
读书笔记|<金字塔原理>_第一章 [章节]--第一章为什么要用金字塔结构 [讲了什么] 1.首先是为什么要用金字塔结构? 2.如何将思想组织成金字塔结构? 2.1 归类分组,将思想组织成金 ...
最新文章
- java IO知识总结
- 1.2机器学习基础下--python深度机器学习
- java基础不好框架能学会吗_转行Java能学会吗?零基础学习Java的学习路线
- mysql几个timeout参数_MySQL中 timeout相关参数解析
- 多线程讲座【by lilei005】
- 东方电子全资子公司中标1.065亿元国家电网第三批采购项目
- [转载]Qt之鼠标样式
- 性能测试--jmeter如何发送get请求【3】
- PAIP.http post 400错误
- 计算机无法连接声音怎么办,电脑耳机没声音怎么设置|耳机插电脑没有声音解决方法...
- 磁盘分区方式对比(MBR与GPT)
- 常州2021高考成绩查询,2021年常州高考各高中成绩及本科升学率数据排名及分析...
- 关于pc浏览器浏览外网出现ERR_EMPTY_RESPONSE的问题
- mac 版本navicate 如何安装破解版
- pip的基本命令和使用
- 开源:全球软件产业四大发展趋势之一(转)
- [react] This synthetic event is reused for performance reasons. If you‘re seeing this, you‘re access
- validateFrom用法
- 如何实行反编译,将.class文件转化为.java文件
- 几何原本译林pdf下载_《几何原本》欧几里得_文字版_pdf电子书下载