高斯金字塔 拉普拉斯金字塔_金字塔學入門指南

高斯金字塔 拉普拉斯金字塔

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:

本教程分為三個部分:

  1. Setup

    建立
  2. Implementation

    實作
  3. 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 of Literal prior to Python 3.8.

    typing-extensions —支持在Python 3.8之前使用Literal

  • python-dotenv — Support for dotenv file with settings.

    python-dotenv —支持帶有設置的dotenv文件。

You can install them manually:

您可以手動安裝它們:

# install email-validator
pip install email-validator# install typing-extensions
pip install typing_extensions# install python-dotenv
pip install python-dotenv

or along with pydantic as follows:

或與pydantic一起使用,如下所示:

# install email-validator
pip install pydantic[email]# install typing-extensions
pip install pydantic[typing_extensions]# install python-dotenv
pip install pydantic[dotenv]# install all dependencies
pip 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 datetime
from typing import List, Optional
from 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 values

    dict() —返回模型字段和值的字典

  • json() — returns a JSON string representation dictionary

    json() —返回一個JSON字符串表示字典

  • copy() — returns a deep copy of the model

    copy() —返回模型的深層副本

  • parse_obj() — a utility for loading any object into a model with error handling if the object is not a dictionary

    parse_obj() —如果對象不是字典,則用于通過錯誤處理將任何對象加載到模型中的實用程序

  • parse_raw() — a utility for loading strings of numerous formats

    parse_raw() —用于加載多種格式的字符串的實用程序

  • parse_field() — similar to parse_raw() but meant for files

    parse_field() -類似于parse_raw()但意味著文件

  • from_orm() — loads data into a model from an arbitrary class

    from_orm() —將數據從任意類加載到模型中

  • schema() — returns a dictionary representing the model as JSON schema

    schema() —返回一個將模型表示為JSON模式的字典

  • schema_json() — returns a JSON string representation of schema()

    schema_json() —返回schema()的JSON字符串表示形式

  • construct() — a class method for creating models without running validation

    construct() —一種無需運行驗證即可創建模型的類方法

  • __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.date

    datetime.date
  • datetime.time

    datetime.time
  • datetime.datetime

    datetime.datetime
  • datetime.timedelta

    datetime.timedelta
  • typing.Any

    打字
  • typing.TypeVar

    Type.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.IPv4Address

    ipaddress.IPv4地址
  • ipaddress.IPv4Interface

    ipaddress.IPv4接口
  • ipaddress.IPv4Network

    ipaddress.IPv4網絡
  • ipaddress.IPv6Address

    ipaddress.IPv6地址
  • ipaddress.IPv6Interface

    ipaddress.IPv6接口
  • ipaddress.IPv6Network

    ipaddress.IPv6網絡
  • enum.Enum

    枚舉
  • enum.IntEnum

    枚舉
  • decimal.Decimal

    十進制。十進制
  • pathlib.Path

    路徑庫
  • uuid.UUID

    uuid.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,因為它僅接受TrueFalse作為輸入。

驗證器 (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 datetime
from typing import List, Optional
from 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 TypesStrict 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位數字作為idconfirm_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.pswp.cn/news/389127.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389127.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389127.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基本排序算法

插入排序 基本思想&#xff1a;把待排序列表分為已排和未排序兩部分&#xff0c;從未排序左邊取值&#xff0c;按順序從已排序的右端開始對比插入到相應的位置。 java代碼實現 private void insertSort(int[] arr){int i, j;int temp;for(i 0; i < arr.length; i){temp …

自定義版本更新彈窗

目錄介紹 1.Animation和Animator區別 2.Animation運行原理和源碼分析 2.1 基本屬性介紹2.2 如何計算動畫數據2.3 什么是動畫更新函數2.4 動畫數據如何存儲2.5 Animation的調用 3.Animator運行原理和源碼分析 3.1 屬性動畫的基本屬性3.2 屬性動畫新的概念3.3 PropertyValuesHold…

《SQL Server 2008從入門到精通》--20180716

1.鎖 當多個用戶同時對同一個數據進行修改時會產生并發問題&#xff0c;使用事務就可以解決這個問題。但是為了防止其他用戶修改另一個還沒完成的事務中的數據&#xff0c;就需要在事務中用到鎖。 SQL Server 2008提供了多種鎖模式&#xff1a;排他鎖&#xff0c;共享鎖&#x…

googleearthpro打開沒有地球_嫦娥五號成功著陸地球!為何嫦娥五號返回時會燃燒,升空卻不會?...

目前&#xff0c;嫦娥五號已經帶著月壤成功降落到地球上&#xff0c;創造了中國航天的又一里程碑。嫦娥五號這一路走來&#xff0c;困難重重&#xff0c;但都被我國航天科技人員逐一克服&#xff0c;最終圓滿地完成了嫦娥五號的月球采樣返回地球任務。嫦娥五號最后這一步走得可…

語言認知偏差_我們的認知偏差正在破壞患者的結果數據

語言認知偏差How do we know if we are providing high-quality care? The answer to this question is sought by a multitude of parties: patients, clinicians, educators, legislators, and insurance companies. Unfortunately, it’s not easy to determine. There is …

android 打包相關問題記錄

Android 中的打包配置在build.gradle文件中&#xff0c;下面對該文件的內容做一下記錄。 buildscript {repositories {jcenter()}dependencies {classpath com.android.tools.build:gradle:2.2.0} } 這里生命了倉庫的位置&#xff0c;依賴gradle的版本。 android{} android {…

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲

本文將引導你使用XNA Game Studio Express一步一步地創建一個簡單的游戲 第1步: 安裝軟件 第2步: 創建新項目 第3步: 查看代碼 第4步: 加入一個精靈 第5步: 使精靈可以移動和彈跳 第6步: 繼續嘗試! 完整的實例 第1步: 安裝軟件在動手之前,先確定你已經安裝了所需的軟件,其中包…

C#中實現對象的深拷貝

深度拷貝指的是將一個引用類型&#xff08;包含該類型里的引用類型&#xff09;拷貝一份(在內存中完完全全是兩個對象&#xff0c;沒有任何引用關系)..........  直接上代碼&#xff1a; 1 /// <summary>2 /// 對象的深度拷貝&#xff08;序列化的方式&#xf…

Okhttp 源碼解析

HTTP及okhttp的優勢 http結構 請求頭 列表內容表明本次請求的客戶端本次請求的cookie本次請求希望返回的數據類型本次請求是否采用數據壓縮等等一系列設置 請求體 指定本次請求所使用的方法請求所使用的方法 響應頭 - 服務器標識 - 狀態碼 - 內容編碼 - cookie 返回給客…

python中定義數據結構_Python中的數據結構。

python中定義數據結構I remembered the day when I made up my mind to learn python then the very first things I learned about data types and data structures. So in this article, I would like to discuss different data structures in python.我記得當初下定決心學習…

python實訓英文_GitHub - MiracleYoung/You-are-Pythonista: 匯聚【Python應用】【Python實訓】【Python技術分享】等等...

You-are-Pythonista匯聚【從零單排】【實戰項目】【數據科學】【自然語言處理】【計算機視覺】【面試題系列】【大航海】【Python應用】【錯題集】【技術沙龍】【內推渠道】等等【人人都是Pythonista】由公眾號【Python專欄】推出&#xff0c;請認準唯一標識&#xff1a;請仔細…

java電子商務系統源碼 Spring MVC+mybatis+spring cloud+spring boot+spring security

鴻鵠云商大型企業分布式互聯網電子商務平臺&#xff0c;推出PC微信APP云服務的云商平臺系統&#xff0c;其中包括B2B、B2C、C2C、O2O、新零售、直播電商等子平臺。 分布式、微服務、云架構電子商務平臺 java b2b2c o2o 技術解決方案 開發語言&#xff1a; java、j2ee 數據庫&am…

Go語言實現FastDFS分布式存儲系統WebAPI網關

前言 工作需要&#xff0c;第一次使用 Go 來實戰項目。 需求&#xff1a;采用 golang 實現一個 webapi 的中轉網關&#xff0c;將一些資源文件通過 http 協議上傳至 FastDFS 分布式文件存儲系統。 一、FastDFS 與 golang 對接的代碼 github&#xff1a;https://github.com/weil…

builder 模式

首先提出幾個問題&#xff1a; 什么是Builder模式&#xff1f;為什么要使用Builder模式&#xff1f;它的優點是什么&#xff0c;那缺點呢&#xff1f;什么情況下使用Builder模式&#xff1f; 關于Builder模式在代碼中用的很多&#xff0c;比如AlertDialog, OkHttpClient等。一…

工作失職的處理決定_工作失職的處理決定

精品文檔2016全新精品資料-全新公文范文-全程指導寫作–獨家原創1/3工作失職的處理決定失職是指工作人員對本職工作不認真負責&#xff0c;未依照規定履行自己的職務&#xff0c;致使單位或服務對象造成損失的行為。關于工作失職的處理決定該怎么寫呢?下面學習啦小編給大家帶來…

venn diagram_Venn Diagram Python軟件包:Vennfig

venn diagram目錄 (Table of Contents) Introduction 介紹 Installation 安裝 Default Functions 默認功能 Parameters 參量 Examples 例子 Conclusion 結論 介紹 (Introduction) In the last article, I showed how to draw basic Venn diagrams using matplotlib_venn.在上一…

應用程序的主入口點應用程序的主入口點應用程序的主入口點

/// <summary>/// 應用程序的主入口點。/// </summary>[STAThread]static void Main(string[] args){Stream stream Assembly.GetExecutingAssembly().GetManifestResourceStream("CapApp.TestApp.exe");byte[] bs new byte[stream.Length];stream.Rea…

創夢天地通過聆訊:上半年經營利潤1.3億 騰訊持股超20%

雷帝網 雷建平 11月23日報道時隔半年后&#xff0c;樂逗游戲母公司創夢天地終于通過上市聆訊&#xff0c;這意味著創夢天地很快將在港交所上市。創夢天地聯合保薦人包括瑞信、招商證券國際、中金公司。當前&#xff0c;創夢天地運營的游戲包括《夢幻花園》、《快樂點點消》、《…

PyCharm之python書寫規范--消去提示波浪線

強迫癥患者面對PyCharm的波浪線是很難受的&#xff0c;針對如下代碼去除PyCharm中的波浪線&#xff1a; # _*_coding:utf-8_*_ # /usr/bin/env python3 A_user "lin" A_password "lin123"for i in range(3): # 循環次數為3name input("請輸入你的…

關于java static 關鍵字

當我們創建類時會指出哪個類的對象的外觀與行為。 一般的流程是用new 創建這個類的對象&#xff0c;然后生成數據的存儲空間&#xff0c;并使用相應的方法。 但以下兩種情況不太適合這個流程&#xff1a; 只想用一個存儲區域來保存一個特定的數據—–無論要創建多少個對象&a…