feat: 新增技能扩展N8一章相关示例源码
This commit is contained in:
450
code/newsletter/N8/typehints.ipynb
Normal file
450
code/newsletter/N8/typehints.ipynb
Normal file
@@ -0,0 +1,450 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# basis"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## built-in types"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"name: str = \"100gle\"\n",
|
||||
"age: int = 18\n",
|
||||
"salary: float = 3.1415926\n",
|
||||
"is_male: bool = True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## container types"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing import List, Tuple, Dict, Set"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"numbers: List[int] = [1, 2, 3, 4, 5]\n",
|
||||
"auth: Tuple[str] = (\"username\", \"password\")\n",
|
||||
"\n",
|
||||
"data: Dict[str, str] = dict(\n",
|
||||
" name=\"100gle\",\n",
|
||||
" site=\"https://sspai.com/u/100gle/updates\",\n",
|
||||
")\n",
|
||||
"alphabet: Set[str] = set('abcde')\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Hello', 'world!']"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def concat(data: List[str], sep: str = \" \") -> str:\n",
|
||||
" return sep.join(data)\n",
|
||||
"\n",
|
||||
"def split(s: str, sep: str = \" \") -> List[str]:\n",
|
||||
" return s.split(sep)\n",
|
||||
"\n",
|
||||
"s = concat(data=[\"Hello\", \"world!\"], sep=\",\")\n",
|
||||
"split(s, sep=\",\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Advanced "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Union type"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, ('str',), False]"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import Union\n",
|
||||
"\n",
|
||||
"container: List[Union[bool, int, Tuple[str]]] = [1, tuple([\"str\"]), False]\n",
|
||||
"container"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, ('str',), False]"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"MyUnionType = Union[bool, int, Tuple[str]]\n",
|
||||
"container: List[MyUnionType] = [1, tuple([\"str\"]), False]\n",
|
||||
"container"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Optional type"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Hello, world!\n",
|
||||
"Hello, 100gle\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import Optional\n",
|
||||
"\n",
|
||||
"def greet(name: Optional[str] = None) -> str:\n",
|
||||
" if not name:\n",
|
||||
" return \"Hello, world!\"\n",
|
||||
" return f\"Hello, {name}\"\n",
|
||||
"\n",
|
||||
"print(greet())\n",
|
||||
"print(greet(\"100gle\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Any and Generic type"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1\n",
|
||||
"[1, 2, 3, 4]\n",
|
||||
"<object object at 0x110f2cbb0>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import Any\n",
|
||||
"\n",
|
||||
"def echo(val: Any) -> None:\n",
|
||||
" print(f\"{val}\")\n",
|
||||
"\n",
|
||||
"echo(1)\n",
|
||||
"echo([1,2,3,4])\n",
|
||||
"echo(object())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1\n",
|
||||
"[1, 2, 3, 4]\n",
|
||||
"<object object at 0x110f2cc50>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import TypeVar\n",
|
||||
"\n",
|
||||
"T = TypeVar(\"T\", str, bytes)\n",
|
||||
"\n",
|
||||
"def echo(val: T) -> None:\n",
|
||||
" print(f\"{val}\")\n",
|
||||
"\n",
|
||||
"echo(1)\n",
|
||||
"echo([1,2,3,4])\n",
|
||||
"echo(object())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Foo\n",
|
||||
"[1, 2, 3, 4]\n",
|
||||
"<object object at 0x110f2cb80>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import TypeVar\n",
|
||||
"\n",
|
||||
"T = TypeVar(\"T\", bound=str)\n",
|
||||
"\n",
|
||||
"def echo(val: T) -> None:\n",
|
||||
" print(f\"{val}\")\n",
|
||||
"\n",
|
||||
"echo(\"Foo\")\n",
|
||||
"echo([1,2,3,4])\n",
|
||||
"echo(object())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Callable Type"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Start calling foo...\n",
|
||||
"foo\n",
|
||||
"Ending foo...\n",
|
||||
"Start calling bar...\n",
|
||||
"bar\n",
|
||||
"Ending bar...\n",
|
||||
"Start calling baz...\n",
|
||||
"baz\n",
|
||||
"Ending baz...\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import Callable\n",
|
||||
"from functools import wraps\n",
|
||||
"\n",
|
||||
"def logger(func: Callable):\n",
|
||||
" @wraps(func)\n",
|
||||
" def wrapper(*args, **kwargs):\n",
|
||||
" print(f\"Start calling {func.__name__}...\")\n",
|
||||
" result = func(*args, **kwargs)\n",
|
||||
" print(f\"Ending {func.__name__}...\")\n",
|
||||
" return result\n",
|
||||
" return wrapper\n",
|
||||
"\n",
|
||||
"@logger\n",
|
||||
"def foo():\n",
|
||||
" print(\"foo\")\n",
|
||||
"\n",
|
||||
"@logger\n",
|
||||
"def bar():\n",
|
||||
" print(\"bar\")\n",
|
||||
"\n",
|
||||
"@logger\n",
|
||||
"def baz():\n",
|
||||
" print(\"baz\")\n",
|
||||
"\n",
|
||||
"funcs = [foo, bar, baz]\n",
|
||||
"for func in funcs:\n",
|
||||
" func()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"proxying for add...\n",
|
||||
"3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import Callable, TypeVar, Union\n",
|
||||
"\n",
|
||||
"Number = Union[int, float]\n",
|
||||
"T = TypeVar(\"T\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def proxy(func: Callable[[int, float], T], *args, **kwargs):\n",
|
||||
" print(f\"proxying for {func.__name__}...\")\n",
|
||||
" return func(*args, **kwargs)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def add(x: Number, y: Number) -> Number:\n",
|
||||
" return x + y\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"print(proxy(add, 1, 2))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Start calling foo...\n",
|
||||
"foo\n",
|
||||
"Ending foo...\n",
|
||||
"Start calling bar...\n",
|
||||
"bar\n",
|
||||
"Ending bar...\n",
|
||||
"Start calling baz...\n",
|
||||
"baz\n",
|
||||
"Ending baz...\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from typing import Callable, TypeVar\n",
|
||||
"from functools import wraps\n",
|
||||
"\n",
|
||||
"T = TypeVar(\"T\")\n",
|
||||
"\n",
|
||||
"def logger(func: Callable[..., T]) -> Callable[..., T]:\n",
|
||||
" @wraps(func)\n",
|
||||
" def wrapper(*args, **kwargs):\n",
|
||||
" print(f\"Start calling {func.__name__}...\")\n",
|
||||
" result = func(*args, **kwargs)\n",
|
||||
" print(f\"Ending {func.__name__}...\")\n",
|
||||
" return result\n",
|
||||
" return wrapper\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@logger\n",
|
||||
"def foo():\n",
|
||||
" print(\"foo\")\n",
|
||||
"\n",
|
||||
"@logger\n",
|
||||
"def bar():\n",
|
||||
" print(\"bar\")\n",
|
||||
"\n",
|
||||
"@logger\n",
|
||||
"def baz():\n",
|
||||
" print(\"baz\")\n",
|
||||
"\n",
|
||||
"funcs = [foo, bar, baz]\n",
|
||||
"for func in funcs:\n",
|
||||
" func()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.10.4 64-bit",
|
||||
"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.10.4"
|
||||
},
|
||||
"orig_nbformat": 4,
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user