最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 中的Schema數(shù)據(jù)結(jié)構(gòu)及類型校驗(yàn)詳解

 更新時(shí)間:2023年11月21日 09:26:37   作者:jruing  
schema?是一個(gè)簡(jiǎn)單而強(qiáng)大的庫(kù),用于定義和驗(yàn)證 Python 數(shù)據(jù)結(jié)構(gòu)的約束,使用?schema?庫(kù)來(lái)執(zhí)行數(shù)據(jù)結(jié)構(gòu)的校驗(yàn),本文給大家介紹Python 中的Schema數(shù)據(jù)結(jié)構(gòu)及類型校驗(yàn),感興趣的朋友一起看看吧

使用 schema 庫(kù)來(lái)執(zhí)行數(shù)據(jù)結(jié)構(gòu)的校驗(yàn)。schema 是一個(gè)簡(jiǎn)單而強(qiáng)大的庫(kù),用于定義和驗(yàn)證 Python 數(shù)據(jù)結(jié)構(gòu)的約束

And

And 代表必選,數(shù)據(jù)結(jié)構(gòu)里必須包含這個(gè) schema,如下方聲明了 name ,則代表這個(gè)name必須存在與字典中

from schema import Schema, And, SchemaError
user_schema = Schema([
    {
        "name": And(str)
    }
])
user_data_1 = [{
    "name": "jruing",
}]
user_data_2 = [{
    "name": 666,
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
==========調(diào)用結(jié)果==========
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing'}]
數(shù)據(jù)校驗(yàn)異常user_result_2:Or({'name': And(<class 'str'>)}) did not validate {'name': 666}
Key 'name' error:
666 should be instance of 'str'

Or

Or 代表值的類型必須為某兩個(gè)類型,比如int 或 float,tuplelist

from schema import Schema, And, SchemaError, Or
user_schema = Schema([
    {
        "name": And(str),
        "money": Or(int,float)
    }
])
user_data_1 = [{
    "name": "jruing",
    "money": 1000,
}]
user_data_2 = [{
    "name": "jruing",
    "money": 1000.1,
}]
user_data_3 = [{
    "name": "jruing",
    "money": "1000.1",
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
try:
    user_result_3 = user_schema.validate(user_data_3)
    print(f"數(shù)據(jù)校驗(yàn)user_result_3:{user_result_3}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_3:{e}")
==========調(diào)用結(jié)果==========
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing', 'money': 1000, 'addr': '中國(guó)', 'country': '中國(guó)', 'email': '123456@qq.com'}]
數(shù)據(jù)校驗(yàn)異常user_result_2:Or({'name': And(<class 'str'>), 'money': Or(<class 'int'>, <class 'float'>), Optional('addr'): And(<class 'str'>), Optional('email'): And(<class 'str'>, Regex('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$', flags=re.IGNORECASE)), 'country': Const('中國(guó)')}) did not validate {'name': 'jruing', 'money': 1000.1, 'addr': '1111', 'country': '山西', 'email': '123456'}
Key 'country' error:
'中國(guó)' does not match '山西'

Const

Const 代表值必須為指定的某個(gè)常量,比如下面的 country必須為中國(guó)

import re
from schema import Schema, And, SchemaError, Or, Optional, Regex, Const
user_schema = Schema([
    {
        "name": And(str),
        "money": Or(int, float),
        Optional("addr"): And(str),
        Optional("email"): And(str, Regex(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', flags=re.I)),
        "country": Const("中國(guó)")
    }
])
user_data_1 = [{
    "name": "jruing",
    "money": 1000,
    "addr": "中國(guó)",
    "country": "中國(guó)",
    "email": "123456@qq.com"
}]
user_data_2 = [{
    "name": "jruing",
    "money": 1000.1,
    "addr": "1111",
    "country": "山西",
    "email": "123456"
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
==========調(diào)用結(jié)果==========
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing', 'money': 1000, 'addr': '中國(guó)', 'country': '中國(guó)', 'email': '123456@qq.com'}]
數(shù)據(jù)校驗(yàn)異常user_result_2:Or({'name': And(<class 'str'>), 'money': Or(<class 'int'>, <class 'float'>), Optional('addr'): And(<class 'str'>), Optional('email'): And(<class 'str'>, Regex('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$', flags=re.IGNORECASE)), 'country': Const('中國(guó)')}) did not validate {'name': 'jruing', 'money': 1000.1, 'addr': '1111', 'country': '山西', 'email': '123456'}
Key 'country' error:
'中國(guó)' does not match '山西'

Optional

Optional 代表這個(gè)key或者元素為非必選,可有可無(wú)

from schema import Schema, And, SchemaError, Or, Optional
user_schema = Schema([
    {
        "name": And(str),
        "money": Or(int, float),
        Optional("addr"): And(str)
    }
])
user_data_1 = [{
    "name": "jruing",
    "money": 1000,
    "addr": "中國(guó)"
}]
user_data_2 = [{
    "name": "jruing",
    "money": 1000.1,
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
==========調(diào)用結(jié)果==========
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing', 'money': 1000, 'addr': '中國(guó)'}]
數(shù)據(jù)校驗(yàn)user_result_2:[{'name': 'jruing', 'money': 1000.1}]

Use

Use 函數(shù)允許你在驗(yàn)證前對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換。這對(duì)于在驗(yàn)證之前對(duì)數(shù)據(jù)進(jìn)行清理、格式化或其他操作非常有用。

import re
from schema import Schema, And, SchemaError, Or, Optional, Regex, Const, Use
user_schema = Schema([
    {
        "name": And(str),
        "money": Or(int, float),
        "age": Use(int),
        Optional("addr"): And(str),
        Optional("email"): And(str, Regex(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', flags=re.I)),
        Optional("country"): Const("中國(guó)")
    }
])
user_data_1 = [{
    "name": "jruing",
    "money": 1000,
    "age": 11,
    "addr": "中國(guó)",
    "country": "中國(guó)",
    "email": "123456@qq.com"
}]
user_data_2 = [{
    "name": "jruing",
    "money": 1000.1,
    "age": "18",
    "addr": "1111",
    "email": "123456@qq.com"
}]
user_data_3 = [{
    "name": "jruing",
    "money": 1000.1,
    "age": "fff",
    "addr": "1111",
    "email": "123456@qq.com"
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
try:
    user_result_3 = user_schema.validate(user_data_3)
    print(f"數(shù)據(jù)校驗(yàn)user_result_3:{user_result_3}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_3:{e}")
==========調(diào)用結(jié)果==========
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing', 'money': 1000, 'age': 11, 'addr': '中國(guó)', 'country': '中國(guó)', 'email': '123456@qq.com'}]
數(shù)據(jù)校驗(yàn)user_result_2:[{'name': 'jruing', 'money': 1000.1, 'age': 18, 'addr': '1111', 'email': '123456@qq.com'}]
數(shù)據(jù)校驗(yàn)異常user_result_3:Or({'name': And(<class 'str'>), 'money': Or(<class 'int'>, <class 'float'>), 'age': Use(<class 'int'>), Optional('addr'): And(<class 'str'>), Optional('email'): And(<class 'str'>, Regex('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$', flags=re.IGNORECASE)), Optional('country'): Const('中國(guó)')}) did not validate {'name': 'jruing', 'money': 1000.1, 'age': 'fff', 'addr': '1111', 'email': '123456@qq.com'}
Key 'age' error:
int('fff') raised ValueError("invalid literal for int() with base 10: 'fff'")

Regex

通過(guò)正則表達(dá)式,對(duì)值進(jìn)行匹配校驗(yàn),常用的就是郵箱,手機(jī)號(hào)等場(chǎng)景

import re
from schema import Schema, And, SchemaError, Or, Optional, Regex
user_schema = Schema([
    {
        "name": And(str),
        "money": Or(int, float),
        Optional("addr"): And(str),
        Optional("email"): And(str, Regex(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', flags=re.I))
    }
])
user_data_1 = [{
    "name": "jruing",
    "money": 1000,
    "addr": "中國(guó)",
    "email": "123456@qq.com"
}]
user_data_2 = [{
    "name": "jruing",
    "money": 1000.1,
    "addr": "1111",
    "email": "123456"
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
==========調(diào)用結(jié)果==========
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing', 'money': 1000, 'addr': '中國(guó)', 'email': '123456@qq.com'}]
數(shù)據(jù)校驗(yàn)異常user_result_2:Or({'name': And(<class 'str'>), 'money': Or(<class 'int'>, <class 'float'>), Optional('addr'): And(<class 'str'>), Optional('email'): And(<class 'str'>, Regex('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$', flags=re.IGNORECASE))}) did not validate {'name': 'jruing', 'money': 1000.1, 'addr': '1111', 'email': '123456'}
Key 'email' error:
Regex('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$', flags=re.IGNORECASE) does not match '123456'

Forbidden

Forbidder 允許你定義一些不被允許的值,如果數(shù)據(jù)中包含這些值,驗(yàn)證將失敗,下面的例子表示密碼 password字段的值不允許設(shè)置為123456

import re
from schema import Schema, And, SchemaError, Or, Optional, Regex, Const, Use, Forbidden
user_schema = Schema([
    {
        "name": And(str),
        "money": Or(int, float),
        "age": Use(int),
        Optional("addr"): And(str),
        Optional("email"): And(str, Regex(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', flags=re.I)),
        Optional("country"): Const("中國(guó)"),
        "password": And(str, Forbidden("123456"))
    }
])
user_data_1 = [{
    "name": "jruing",
    "money": 1000,
    "age": 11,
    "addr": "中國(guó)",
    "country": "中國(guó)",
    "email": "123456@qq.com",
    "password": "123456"
}]
user_data_2 = [{
    "name": "jruing",
    "money": 1000.1,
    "age": "18",
    "addr": "1111",
    "email": "123456@qq.com",
    "password": "1234561"
}]
try:
    user_result_1 = user_schema.validate(user_data_1)
    print(f"數(shù)據(jù)校驗(yàn)user_result_1:{user_result_1}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_1:{e}")
try:
    user_result_2 = user_schema.validate(user_data_2)
    print(f"數(shù)據(jù)校驗(yàn)user_result_2:{user_result_2}")
except SchemaError as e:
    print(f"數(shù)據(jù)校驗(yàn)異常user_result_2:{e}")
==========調(diào)用結(jié)果==========    
數(shù)據(jù)校驗(yàn)user_result_1:[{'name': 'jruing', 'money': 1000, 'age': 11, 'addr': '中國(guó)', 'country': '中國(guó)', 'email': '123456@qq.com', 'password': '123456'}]
數(shù)據(jù)校驗(yàn)異常user_result_2:Or({'name': And(<class 'str'>), 'money': Or(<class 'int'>, <class 'float'>), 'age': Use(<class 'int'>), Optional('addr'): And(<class 'str'>), Optional('email'): And(<class 'str'>, Regex('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$', flags=re.IGNORECASE)), Optional('country'): Const('中國(guó)'), 'password': And(<class 'str'>, Forbidden('123456'))}) did not validate {'name': 'jruing', 'money': 1000.1, 'age': '18', 'addr': '1111', 'email': '123456@qq.com', 'password': '1234561'}
Key 'password' error:
'123456' does not match '1234561'

到此這篇關(guān)于Python 中的Schema數(shù)據(jù)結(jié)構(gòu)及類型校驗(yàn)的文章就介紹到這了,更多相關(guān)Python Schema數(shù)據(jù)結(jié)構(gòu)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何利用python多線程爬取天氣網(wǎng)站圖片并保存

    如何利用python多線程爬取天氣網(wǎng)站圖片并保存

    最近做個(gè)天 氣方面的APP需要用到一些天氣數(shù)據(jù),所以下面這篇文章主要給大家介紹了關(guān)于如何利用python多線程爬取天氣網(wǎng)站圖片并保存的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • python聊天室(雖然很簡(jiǎn)潔,但是可以用)

    python聊天室(雖然很簡(jiǎn)潔,但是可以用)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • python單線程文件傳輸?shù)膶?shí)例(C/S)

    python單線程文件傳輸?shù)膶?shí)例(C/S)

    今天小編就為大家分享一篇python單線程文件傳輸?shù)膶?shí)例(C/S),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 利用Python實(shí)現(xiàn)獲取照片位置信息

    利用Python實(shí)現(xiàn)獲取照片位置信息

    Python中的exifread庫(kù),不僅僅是 GPS 信息,幾乎能獲得圖片的所有信息。本文就將利用這個(gè)庫(kù)實(shí)現(xiàn)獲取照片位置信息,感興趣的可以了解一下
    2022-08-08
  • python類方法中的self關(guān)鍵字使用

    python類方法中的self關(guān)鍵字使用

    這篇文章主要介紹了python類方法中的self關(guān)鍵字使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • python利用selenium進(jìn)行瀏覽器爬蟲

    python利用selenium進(jìn)行瀏覽器爬蟲

    這篇文章主要介紹了python項(xiàng)目實(shí)戰(zhàn)之利用selenium進(jìn)行瀏覽器爬蟲,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • Python深入學(xué)習(xí)之內(nèi)存管理

    Python深入學(xué)習(xí)之內(nèi)存管理

    這篇文章主要介紹了Python深入學(xué)習(xí)之內(nèi)存管理,本文比較詳細(xì)的講解了Python的內(nèi)存管理相關(guān)知識(shí),需要的朋友可以參考下
    2014-08-08
  • Python接口自動(dòng)化淺析如何處理接口依賴

    Python接口自動(dòng)化淺析如何處理接口依賴

    本文主要介紹如何提取token、將token作為類屬性全局調(diào)用及充值接口如何攜帶token進(jìn)行請(qǐng)求,其他接口調(diào)用的前提條件是當(dāng)前用戶必須是登錄狀態(tài),如何處理接口依賴
    2021-08-08
  • python用字典統(tǒng)計(jì)單詞或漢字詞個(gè)數(shù)示例

    python用字典統(tǒng)計(jì)單詞或漢字詞個(gè)數(shù)示例

    這篇文章主要介紹了python用字典統(tǒng)計(jì)單詞或漢字詞個(gè)數(shù)示例,需要的朋友可以參考下
    2014-04-04
  • numpy庫(kù)ndarray多維數(shù)組的維度變換方法(reshape、resize、swapaxes、flatten)

    numpy庫(kù)ndarray多維數(shù)組的維度變換方法(reshape、resize、swapaxes、flatten)

    這篇文章主要介紹了numpy庫(kù)ndarray多維數(shù)組的維度變換方法(reshape、resize、swapaxes、flatten),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論

临澧县| 泾川县| 迁安市| 镇安县| 巴林左旗| 郓城县| 曲麻莱县| 正蓝旗| 永济市| 永吉县| 曲沃县| 稻城县| 济源市| 瑞丽市| 年辖:市辖区| 武邑县| 大新县| 武清区| 维西| 安塞县| 米泉市| 赫章县| 道真| 巨野县| 甘肃省| 泽州县| 依兰县| 蒙城县| 桃源县| 巍山| 亳州市| 中牟县| 淅川县| 嘉义县| 新邵县| 文登市| 札达县| 大关县| 皋兰县| 临安市| 黔东|