feat: 新增技能扩展N10一章相关示例源码
This commit is contained in:
516
code/newsletter/N10/pydantic.ipynb
Normal file
516
code/newsletter/N10/pydantic.ipynb
Normal file
@@ -0,0 +1,516 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Item(name='iPhone', price=0, number=0, categories=[Category(name='', level=1)])"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"\n",
|
||||
"from pydantic import BaseModel\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Category(BaseModel):\n",
|
||||
" name: str = \"\"\n",
|
||||
" level: int = 1\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Item(BaseModel):\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0\n",
|
||||
" categories: t.List[Category] = [Category()]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Item(name=\"iPhone\")\n",
|
||||
"Item(name='iPhone', price=0, number=0, categories=[Category(name='', level=1)])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 额外的类型注解"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pydantic import BaseModel, FilePath, HttpUrl, IPvAnyAddress, PostgresDsn\n",
|
||||
"from pydantic.color import Color\n",
|
||||
"\n",
|
||||
"class FileSystem(BaseModel):\n",
|
||||
" path: FilePath\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Link(BaseModel):\n",
|
||||
" url: HttpUrl\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Network(BaseModel):\n",
|
||||
" ip: IPvAnyAddress\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class PostgreDatabase(BaseModel):\n",
|
||||
" dsn: PostgresDsn\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class WebColor(BaseModel):\n",
|
||||
" value: Color"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Item(name='iphone', number=0)"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from pydantic import constr, conint\n",
|
||||
"\n",
|
||||
"class Item(BaseModel):\n",
|
||||
" name: constr(strip_whitespace=True, min_length=2, max_length=10)\n",
|
||||
" number: conint(ge=0) = 0\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Item(name=\" iphone \")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Item(name=\"iPhone\", number=-1) # raise ValidationError here!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 数据校验"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass\n",
|
||||
"from pydantic import BaseModel, validator\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Category:\n",
|
||||
" level: int = 1\n",
|
||||
" name: str = \"\"\n",
|
||||
"\n",
|
||||
"class Item(BaseModel):\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0\n",
|
||||
" categories: t.List[Category] = [Category()]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" @validator(\"price\", \"number\")\n",
|
||||
" def prevent_negative_number(cls, v):\n",
|
||||
" if v < 0:\n",
|
||||
" raise ValueError(\"can't set a negative number\")\n",
|
||||
" return v"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Item(name=\"iPhone\", price=-1) # raise ValidationError here!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import field\n",
|
||||
"\n",
|
||||
"from pydantic import validator\n",
|
||||
"from pydantic.dataclasses import dataclass # <-- use pydantic.dataclasses.dataclass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Category:\n",
|
||||
" name: str = \"\"\n",
|
||||
" level: int = 1\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def default_categories():\n",
|
||||
" return [Category()]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Item:\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float] = field(default=0)\n",
|
||||
" number: int = field(default=0)\n",
|
||||
" categories: t.List[Category] = field(default_factory=default_categories)\n",
|
||||
"\n",
|
||||
" @validator(\"price\", \"number\")\n",
|
||||
" def prevent_negative_number(cls, v):\n",
|
||||
" if v < 0:\n",
|
||||
" raise ValueError(\"can't set a negative number\")\n",
|
||||
" return v"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"\n",
|
||||
"from pydantic import BaseModel, validator\n",
|
||||
"from pydantic.dataclasses import dataclass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Category:\n",
|
||||
" name: str = \"\"\n",
|
||||
" level: int = 1\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class ItemCode(str):\n",
|
||||
" @classmethod\n",
|
||||
" def __get_validators__(cls):\n",
|
||||
" yield cls.validate\n",
|
||||
"\n",
|
||||
" @classmethod\n",
|
||||
" def validate(cls, v):\n",
|
||||
" if not v.startswith(\"ITEM\"):\n",
|
||||
" raise ValueError(\"invalid item code format\")\n",
|
||||
" return v\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Item(BaseModel):\n",
|
||||
" name: str\n",
|
||||
" itemcode: ItemCode\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0\n",
|
||||
" categories: t.List[Category] = [Category()]\n",
|
||||
"\n",
|
||||
" @validator(\"price\", \"number\")\n",
|
||||
" def prevent_negative_number(cls, v):\n",
|
||||
" if v < 0:\n",
|
||||
" raise ValueError(\"can't set a negative number\")\n",
|
||||
" return v"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Item(name='iPhone', itemcode='ITEM31415926', price=0, number=0, categories=[Category(name='', level=1)])"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"Item(name=\"iPhone\", itemcode=\"ITEM31415926\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Item(name=\"iPhone\", itemcode=\"aabbcc\") # raise ValidationError here!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"\n",
|
||||
"from pydantic import BaseModel, validator\n",
|
||||
"from pydantic.dataclasses import dataclass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class ItemValidator:\n",
|
||||
" @classmethod\n",
|
||||
" def prevent_negative_number(cls, v):\n",
|
||||
" if v < 0:\n",
|
||||
" raise ValueError(\"can't set a negative number\")\n",
|
||||
" return v\n",
|
||||
"\n",
|
||||
" @classmethod\n",
|
||||
" def has_itemcode_preffix(cls, v: str):\n",
|
||||
" if not v.startswith(\"ITEM\"):\n",
|
||||
" raise ValueError(\"invalid item code format\")\n",
|
||||
" return v\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Category:\n",
|
||||
" name: str = \"\"\n",
|
||||
" level: int = 1\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Item(BaseModel):\n",
|
||||
" name: str\n",
|
||||
" itemcode: str\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0\n",
|
||||
" categories: t.List[Category] = [Category()]\n",
|
||||
"\n",
|
||||
" check_number = validator(\"price\", \"number\")(ItemValidator.prevent_negative_number)\n",
|
||||
" check_itemcode = validator(\"itemcode\")(ItemValidator.has_itemcode_preffix)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 导出数据类"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"\n",
|
||||
"from pydantic import BaseModel\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Category(BaseModel):\n",
|
||||
" name: str = \"\"\n",
|
||||
" level: int = 1\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Item(BaseModel):\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0\n",
|
||||
" categories: t.List[Category] = [Category()]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"item = Item(\n",
|
||||
" name=\"iPhone\",\n",
|
||||
" price=6999,\n",
|
||||
" number=1000,\n",
|
||||
" categories=[Category(name=\"Phone\")],\n",
|
||||
")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'name': 'iPhone',\n",
|
||||
" 'price': 6999,\n",
|
||||
" 'number': 1000,\n",
|
||||
" 'categories': [{'name': 'Phone', 'level': 1}]}"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"item.dict()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'{\"name\": \"iPhone\", \"price\": 6999, \"number\": 1000, \"categories\": [{\"name\": \"Phone\", \"level\": 1}]}'"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"item.json()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'name': 'iPhone',\n",
|
||||
" 'price': 6999,\n",
|
||||
" 'categories': [{'name': 'Phone', 'level': 1}]}"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"item.dict(exclude={\"number\": True})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 从 ORM 中进行转换"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"import peewee as pw\n",
|
||||
"from pydantic import BaseModel, Field\n",
|
||||
"\n",
|
||||
"db = pw.SqliteDatabase(\":memory:\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class NoteORM(pw.Model):\n",
|
||||
" id = pw.AutoField()\n",
|
||||
" title = pw.CharField()\n",
|
||||
" content = pw.TextField()\n",
|
||||
" tags = pw.CharField()\n",
|
||||
" create_at = pw.DateTimeField(default=datetime.now())\n",
|
||||
"\n",
|
||||
" class Meta:\n",
|
||||
" database = db\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class NoteModel(BaseModel):\n",
|
||||
" id: t.Optional[int] = Field(example=\"null\")\n",
|
||||
" title: str\n",
|
||||
" content: str\n",
|
||||
" tags: t.Optional[t.List[str]] = Field(example=\"null\")\n",
|
||||
"\n",
|
||||
" class Config:\n",
|
||||
" orm_mode = True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'id': None, 'title': '测试', 'content': '# Hello, world\\n', 'tags': ['技术分享']}"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"note_db = NoteORM(title=\"测试\", content=\"# Hello, world\\n\", tags=[\"技术分享\"])\n",
|
||||
"note = NoteModel.from_orm(note_db)\n",
|
||||
"note.dict()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.9.0 ('pandas-startup')",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.0"
|
||||
},
|
||||
"orig_nbformat": 4,
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "13977d4cc82dee5f9d9535ceb495bd0ab12a43c33c664e5f0d53c24cf634b67f"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user