Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
csst-pipeline
csst_proto
Commits
1951ffe5
Commit
1951ffe5
authored
Sep 16, 2023
by
BO ZHANG
🏀
Browse files
tweaks
parent
6cadaf28
Pipeline
#1216
failed with stage
in 0 seconds
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
docs/source/demo_type_annotation.ipynb
View file @
1951ffe5
...
@@ -2,27 +2,25 @@
...
@@ -2,27 +2,25 @@
"cells": [
"cells": [
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "63584611-70c6-45e4-acfb-9bda5204fda0",
"metadata": {},
"source": [
"source": [
"# How to do Python type annotation"
"# How to do Python type annotation"
],
]
"metadata": {
"collapsed": false
},
"id": "697384beaffb53a3"
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "7cc03b6e-5bc4-47e4-b42c-3a22eb11539b",
"metadata": {},
"source": [
"source": [
"## built-in types"
"## built-in types"
],
]
"metadata": {
"collapsed": false
},
"id": "4313baabbbf2ecb"
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"id": "bfaf414a-40f8-4830-8be4-65f43a03d513",
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"a: int = 8 # 整数型标注\n",
"a: int = 8 # 整数型标注\n",
...
@@ -33,104 +31,70 @@
...
@@ -33,104 +31,70 @@
"f: tuple[int, ...] = (1, 2, 3, 4, 5) # 限定类型但不定元素个数的元组型标注\n",
"f: tuple[int, ...] = (1, 2, 3, 4, 5) # 限定类型但不定元素个数的元组型标注\n",
"g: list = [1, 2, 3] # 一般的列表型标注\n",
"g: list = [1, 2, 3] # 一般的列表型标注\n",
"h: list[int] = [1, 2, 3] # 对列表型来说不需要标元素个数,但可以明确要求元素类型"
"h: list[int] = [1, 2, 3] # 对列表型来说不需要标元素个数,但可以明确要求元素类型"
],
]
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-09-16T13:41:58.838293Z",
"start_time": "2023-09-16T13:41:58.835728Z"
}
},
"id": "5fbe06f15bbcd630"
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "ba646556-0bfa-4a8f-8767-9d76e5c44592",
"metadata": {},
"source": [
"source": [
"## `typing` 模块\n",
"## `typing` 模块\n",
"对于一些内建类型来说是可以直接用内建类型来标注的,例如以下两句是相等的"
"对于一些内建类型来说是可以直接用内建类型来标注的,例如以下两句是相等的"
],
]
"metadata": {
"collapsed": false
},
"id": "a06987fbe46de9e4"
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"id": "8759dc5b-01e9-4583-ab27-114ea87315db",
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"l1: list[int] = [1, 2, 3]\n",
"l1: list[int] = [1, 2, 3]\n",
"\n",
"\n",
"from typing import List\n",
"from typing import List\n",
"l2: List[int] = [1, 2, 3]"
"l2: List[int] = [1, 2, 3]"
],
]
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-09-16T13:41:58.845184Z",
"start_time": "2023-09-16T13:41:58.840838Z"
}
},
"id": "c9d78355e0521512"
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "8f2203cb-8e94-406e-b445-809d04f85e26",
"metadata": {},
"source": [
"source": [
"## Union, Optional和Any\n",
"## Union, Optional和Any\n",
"\n",
"\n",
"- Union表示并集,Union[float, int]表示可以是float也可以是int\n",
"- Union表示并集,Union[float, int]表示可以是float也可以是int\n",
"- Optional表示可选,Optional[int]表示可以是None也可以是int\n",
"- Optional表示可选,Optional[int]表示可以是None也可以是int\n",
"- Any表示任意类型"
"- Any表示任意类型"
],
]
"metadata": {
"collapsed": false
},
"id": "8cab70562a62c99"
},
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"id": "4906826b-2817-4a5b-b3ac-a993fa96b315",
"metadata": {},
"source": [
"source": [
"## numpy.typing\n",
"## numpy.typing\n",
"我们经常会用到numpy.ndarray进行科学计算,可以有以下几种标注方式:\n",
"我们经常会用到numpy.ndarray进行科学计算,可以有以下几种标注方式:\n",
"\n",
"\n",
"1. 直接用np.ndarray进行标注, 此时不限定类型(dtype)"
"1. 直接用np.ndarray进行标注, 此时不限定类型(dtype)"
],
]
"metadata": {
"collapsed": false
},
"id": "ca8b1d813d84ad8c"
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"id": "d93f8b14-5f88-4579-a671-1693d04ce144",
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"import numpy as np\n",
"import numpy as np\n",
"\n",
"\n",
"x1: np.ndarray = np.arange(10)"
"x1: np.ndarray = np.arange(10)"
],
]
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-09-16T13:41:58.845533Z",
"start_time": "2023-09-16T13:41:58.841932Z"
}
},
"id": "6ebd28b4f4c84ab4"
},
{
"cell_type": "markdown",
"source": [
"2. 限定类型时不能使用np.ndarray而必须用numpy.typing模块"
],
"metadata": {
"collapsed": false
},
"id": "622cde4d8d3fb34a"
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"id": "14b83562-b6c6-4873-8936-ad9d0c9c0e6f",
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"import numpy.typing as npt\n",
"import numpy.typing as npt\n",
...
@@ -141,48 +105,34 @@
...
@@ -141,48 +105,34 @@
"x5: npt.NDArray[np.float32] = np.arange(10, dtype=np.float32)\n",
"x5: npt.NDArray[np.float32] = np.arange(10, dtype=np.float32)\n",
"x6: npt.NDArray[np.bool_] = np.ones(10, dtype=bool)\n",
"x6: npt.NDArray[np.bool_] = np.ones(10, dtype=bool)\n",
"x7: npt.NDArray[np.complex_] = np.ones(10, dtype=np.complex_)"
"x7: npt.NDArray[np.complex_] = np.ones(10, dtype=np.complex_)"
],
]
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-09-16T13:41:58.849149Z",
"start_time": "2023-09-16T13:41:58.847111Z"
}
},
"id": "36d4e0be053bbd1a"
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "cc6abe41-f8a1-4c9d-8deb-ba7dad66780f",
"metadata": {},
"outputs": [],
"outputs": [],
"source": [],
"source": []
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-09-16T13:41:58.855848Z",
"start_time": "2023-09-16T13:41:58.848965Z"
}
},
"id": "47b6d72d76ee84a1"
}
}
],
],
"metadata": {
"metadata": {
"kernelspec": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3
(ipykernel)
",
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
"language_info": {
"language_info": {
"codemirror_mode": {
"codemirror_mode": {
"name": "ipython",
"name": "ipython",
"version":
2
"version":
3
},
},
"file_extension": ".py",
"file_extension": ".py",
"mimetype": "text/x-python",
"mimetype": "text/x-python",
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython
2
",
"pygments_lexer": "ipython
3
",
"version": "
2.7.6
"
"version": "
3.11.4
"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
...
...
%% Cell type:markdown id:6
97384beaffb53a3
tags:
%% Cell type:markdown id:6
3584611-70c6-45e4-acfb-9bda5204fda0
tags:
# How to do Python type annotation
# How to do Python type annotation
%% Cell type:markdown id:
4313baabbbf2ec
b tags:
%% Cell type:markdown id:
7cc03b6e-5bc4-47e4-b42c-3a22eb11539
b tags:
## built-in types
## built-in types
%% Cell type:code id:
5fbe06f15bbcd630
tags:
%% Cell type:code id:
bfaf414a-40f8-4830-8be4-65f43a03d513
tags:
```
python
```
python
a
:
int
=
8
# 整数型标注
a
:
int
=
8
# 整数型标注
b
:
float
=
1.23
# 浮点型标注
b
:
float
=
1.23
# 浮点型标注
c
:
bool
=
True
# 布尔型标注
c
:
bool
=
True
# 布尔型标注
d
:
tuple
=
(
1
,
2
)
# 元组型标注
d
:
tuple
=
(
1
,
2
)
# 元组型标注
e
:
tuple
[
int
,
float
]
=
(
1
,
2.34
)
# 限定类型和元素个数的元组标注
e
:
tuple
[
int
,
float
]
=
(
1
,
2.34
)
# 限定类型和元素个数的元组标注
f
:
tuple
[
int
,
...]
=
(
1
,
2
,
3
,
4
,
5
)
# 限定类型但不定元素个数的元组型标注
f
:
tuple
[
int
,
...]
=
(
1
,
2
,
3
,
4
,
5
)
# 限定类型但不定元素个数的元组型标注
g
:
list
=
[
1
,
2
,
3
]
# 一般的列表型标注
g
:
list
=
[
1
,
2
,
3
]
# 一般的列表型标注
h
:
list
[
int
]
=
[
1
,
2
,
3
]
# 对列表型来说不需要标元素个数,但可以明确要求元素类型
h
:
list
[
int
]
=
[
1
,
2
,
3
]
# 对列表型来说不需要标元素个数,但可以明确要求元素类型
```
```
%% Cell type:markdown id:
a06987fbe46de9e4
tags:
%% Cell type:markdown id:
ba646556-0bfa-4a8f-8767-9d76e5c44592
tags:
## `typing` 模块
## `typing` 模块
对于一些内建类型来说是可以直接用内建类型来标注的,例如以下两句是相等的
对于一些内建类型来说是可以直接用内建类型来标注的,例如以下两句是相等的
%% Cell type:code id:
c9d78355e0521512
tags:
%% Cell type:code id:
8759dc5b-01e9-4583-ab27-114ea87315db
tags:
```
python
```
python
l1
:
list
[
int
]
=
[
1
,
2
,
3
]
l1
:
list
[
int
]
=
[
1
,
2
,
3
]
from
typing
import
List
from
typing
import
List
l2
:
List
[
int
]
=
[
1
,
2
,
3
]
l2
:
List
[
int
]
=
[
1
,
2
,
3
]
```
```
%% Cell type:markdown id:8
cab70562a62c99
tags:
%% Cell type:markdown id:8
f2203cb-8e94-406e-b445-809d04f85e26
tags:
## Union, Optional和Any
## Union, Optional和Any
-
Union表示并集,Union[float, int]表示可以是float也可以是int
-
Union表示并集,Union[float, int]表示可以是float也可以是int
-
Optional表示可选,Optional[int]表示可以是None也可以是int
-
Optional表示可选,Optional[int]表示可以是None也可以是int
-
Any表示任意类型
-
Any表示任意类型
%% Cell type:markdown id:
ca8b1d813d84ad8c
tags:
%% Cell type:markdown id:
4906826b-2817-4a5b-b3ac-a993fa96b315
tags:
## numpy.typing
## numpy.typing
我们经常会用到numpy.ndarray进行科学计算,可以有以下几种标注方式:
我们经常会用到numpy.ndarray进行科学计算,可以有以下几种标注方式:
1.
直接用np.ndarray进行标注, 此时不限定类型(dtype)
1.
直接用np.ndarray进行标注, 此时不限定类型(dtype)
%% Cell type:code id:
6ebd28b4f4c84ab
4 tags:
%% Cell type:code id:
d93f8b14-5f88-4579-a671-1693d04ce14
4 tags:
```
python
```
python
import
numpy
as
np
import
numpy
as
np
x1
:
np
.
ndarray
=
np
.
arange
(
10
)
x1
:
np
.
ndarray
=
np
.
arange
(
10
)
```
```
%% Cell type:markdown id:622cde4d8d3fb34a tags:
%% Cell type:code id:14b83562-b6c6-4873-8936-ad9d0c9c0e6f tags:
2.
限定类型时不能使用np.ndarray而必须用numpy.typing模块
%% Cell type:code id:36d4e0be053bbd1a tags:
```
python
```
python
import
numpy.typing
as
npt
import
numpy.typing
as
npt
x2
:
npt
.
NDArray
=
np
.
arange
(
10
,
dtype
=
float
)
x2
:
npt
.
NDArray
=
np
.
arange
(
10
,
dtype
=
float
)
x3
:
npt
.
NDArray
[
np
.
int_
]
=
np
.
arange
(
10
,
dtype
=
int
)
x3
:
npt
.
NDArray
[
np
.
int_
]
=
np
.
arange
(
10
,
dtype
=
int
)
x4
:
npt
.
NDArray
[
np
.
float_
]
=
np
.
arange
(
10
,
dtype
=
float
)
x4
:
npt
.
NDArray
[
np
.
float_
]
=
np
.
arange
(
10
,
dtype
=
float
)
x5
:
npt
.
NDArray
[
np
.
float32
]
=
np
.
arange
(
10
,
dtype
=
np
.
float32
)
x5
:
npt
.
NDArray
[
np
.
float32
]
=
np
.
arange
(
10
,
dtype
=
np
.
float32
)
x6
:
npt
.
NDArray
[
np
.
bool_
]
=
np
.
ones
(
10
,
dtype
=
bool
)
x6
:
npt
.
NDArray
[
np
.
bool_
]
=
np
.
ones
(
10
,
dtype
=
bool
)
x7
:
npt
.
NDArray
[
np
.
complex_
]
=
np
.
ones
(
10
,
dtype
=
np
.
complex_
)
x7
:
npt
.
NDArray
[
np
.
complex_
]
=
np
.
ones
(
10
,
dtype
=
np
.
complex_
)
```
```
%% Cell type:code id:
47b6d72d76ee84a1
tags:
%% Cell type:code id:
cc6abe41-f8a1-4c9d-8deb-ba7dad66780f
tags:
```
python
```
python
```
```
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment