feat: 新增技能扩展N10一章相关示例源码
This commit is contained in:
270
code/newsletter/N10/dataclasses.ipynb
Normal file
270
code/newsletter/N10/dataclasses.ipynb
Normal file
@@ -0,0 +1,270 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Item:\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float]\n",
|
||||
" number: int"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass\n",
|
||||
"\n",
|
||||
"@dataclass(repr=False, eq=True, order=False)\n",
|
||||
"class Item:\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float]\n",
|
||||
" number: int"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Item:\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass, field\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Item:\n",
|
||||
" name: str\n",
|
||||
" price: t.Union[int, float] = 0\n",
|
||||
" number: int = 0\n",
|
||||
" categories: t.List[str] = field(default_factory=list)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass, field\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[str] = field(default_factory=list)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Item(name='iPhone 14', price=6999, number=1000, categories=[Category(level=1, name='')])"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import dataclass, field\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Category:\n",
|
||||
" level: int = 1\n",
|
||||
" name: str = \"\"\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",
|
||||
"\n",
|
||||
"Item(\"iPhone 14\", price=6999, number=1000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if sys.version_info >= (3, 10):\n",
|
||||
" from dataclasses import KW_ONLY\n",
|
||||
"\n",
|
||||
" import typing as t\n",
|
||||
" from dataclasses import KW_ONLY, dataclass, field\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" @dataclass\n",
|
||||
" class Category:\n",
|
||||
" level: int = 1\n",
|
||||
" name: str = \"\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" def default_categories():\n",
|
||||
" return [Category()]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" @dataclass\n",
|
||||
" class Item:\n",
|
||||
" name: str\n",
|
||||
" _: KW_ONLY\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)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import typing as t\n",
|
||||
"from dataclasses import asdict, astuple, dataclass, field\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dataclass\n",
|
||||
"class Category:\n",
|
||||
" level: int = 1\n",
|
||||
" name: str = \"\"\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",
|
||||
"\n",
|
||||
"item = Item(\"iPhone 14\", price=6999, number=1000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"('iPhone 14', 6999, 1000, [(1, '')])"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"astuple(item)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'name': 'iPhone 14',\n",
|
||||
" 'price': 6999,\n",
|
||||
" 'number': 1000,\n",
|
||||
" 'categories': [{'level': 1, 'name': ''}]}"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"asdict(item)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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