-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpattern_match.py
125 lines (104 loc) · 2.63 KB
/
pattern_match.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
match expression:
case pattern1: # do something
case pattern2: # do something else
- 文学模式
- 通配符
- 序列
- 字典
- 类
- OR
- AS
Python知否:3.10深度尝鲜 - 模式匹配
Python知否:3.10深度尝鲜 - 模式匹配
介绍模式匹配的基本语法和可能的工程应用。
"""
from typing import Sequence
from dataclasses import dataclass
Number = float | int
# 文学匹配 和 统配符
def weekday(num: int):
match int(num * 1):
case 1:
return "星期一"
case 2:
return "星期二"
case _:
return "哈哈"
def http_error(code: int):
match code:
case 200:
return 'good'
case 401 | 402 | 404:
return '4xx error'
# 对象属性抽取
@dataclass
class Point:
x: int
y: int
@dataclass
class Line:
node1: Point
node2: Point
def location(point):
match point:
case Point(x=0, y=0):
print("Origin is the point's location.")
case Point(x=0, y=y):
print(f"Y={y} and the point is on the y-axis.")
case Point(x=x, y=0):
print(f"X={x} and the point is on the x-axis.")
case Point():
print("The point is located somewhere else on the plane.")
case _:
print("Not a point")
def show_line(line):
match line:
case Line(node1=Point(x=xx, y=yy), node2=n2): # wow!!
print(xx, yy, n2)
match n2:
case Point(x=x,y=y):
print(f"{x = } {y = }")
# 序列匹配,类型匹配
def sum(nums: Sequence[Number]):
match nums:
case []:
return 0
case [int(first) | float(first), *rest]:
return first + sum(rest)
case _:
raise ValueError()
# AS
def check(var):
match var:
case str() as val: # isinstance()
return "String"
case int() as val:
return "String"
# None
# 抽取数据 和 守卫
def data_extract():
user = {
"name": {"first": "Pablo", "last": "Galindo Salgado"},
"title": "Python 3.10 release manager",
"page": 100
}
# match ... case ...
match user:
case {"name": {"first": first_name}, "page": page} if page > 90:
print(first_name)
print(page)
case _:
print('?')
if __name__ == '__main__':
# print(weekday(1))
# print(weekday(2))
# print(weekday(10))
# nums = [1,2,3]
# print(sum(nums))
# print(check([123]))
data_extract()
# p1 = Point(0, 0)
# p2 = Point(1, 1)
# l = Line(p1, p2)
# show_line(l)