-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatch_xml.py
More file actions
178 lines (163 loc) · 6.33 KB
/
Copy pathmatch_xml.py
File metadata and controls
178 lines (163 loc) · 6.33 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env oython3
""" Parse XML using the new match statement
"""
from xml.etree.ElementTree import XML, Element, tostring
### Demo data
# From https://docs.python.org/3/library/xml.etree.elementtree.html
COUNTRY_DATA = """\
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
"""
### Helpers
def error_string(elem, objtype='element'):
return f'could not parse {objtype}: {tostring(elem, encoding="unicode")!r}'
### Functions
def tokenize_xml(data):
tree = XML(data)
return tree
def debug_parse_countries(tree):
countries = {}
for child in tree:
match child:
case Element(tag='country', attrib={'name': name}) as country:
print (f'found country {name}')
for child in country:
neighbors = {}
print (f'processing child element {child!r}')
match child:
case Element(
tag='rank', text=rank):
rank = int(rank)
print (f'rank: {rank}')
case Element(
tag='year', text=year):
year = int(year)
print (f'year ranked: {year}')
case Element(
tag='gdppc', text=gdppc):
gdppc = float(gdppc)
print (f'GDP per capita: {gdppc}')
case Element(
tag='neighbor',
attrib={'name': nb_name, 'direction': nb_direction}):
neighbors[nb_name] = nb_direction
print (f'found neighbor {nb_name} in direction {nb_direction}')
case wrong_data:
raise TypeError(error_string(wrong_data, 'child'))
countries[name] = dict(
rank=rank,
year=year,
gdppc=gdppc,
neighbors=neighbors,
)
case wrong_data:
raise TypeError(error_string(wrong_data, 'child'))
return countries
def parse_countries_1(tree):
countries = {}
for child in tree:
match child:
case Element(tag='country',
attrib={'name': name}) as country:
for child in country:
neighbors = {}
# WARNING: This does not detect missing child elements
match child:
case Element(tag='rank', text=rank):
rank = int(rank)
case Element(tag='year', text=year):
year = int(year)
case Element(tag='gdppc', text=gdppc):
gdppc = float(gdppc)
case Element(
tag='neighbor',
attrib={'name': nb_name,
'direction': nb_direction}):
neighbors[nb_name] = nb_direction
case wrong_data:
raise TypeError(
error_string(wrong_data,
'country element'))
countries[name] = dict(
rank=rank,
year=year,
gdppc=gdppc,
neighbors=neighbors,
)
case wrong_data:
raise TypeError(error_string(wrong_data,
'country'))
return countries
def parse_countries_2(tree):
countries = {}
for child in tree:
match child:
case Element(tag='country',
attrib={'name': name}) as country:
match list(country):
case [
Element(tag='rank', text=rank),
Element(tag='year', text=year),
Element(tag='gdppc', text=gdppc),
*extra,
]:
# Convert types
rank = int(rank)
year = int(year)
gdppc = float(gdppc)
# Parse neighbors
neighbors = {}
for child in extra:
match child:
case Element(
tag='neighbor',
attrib={
'name': nb_name,
'direction': nb_direction}):
neighbors[nb_name] = nb_direction
case wrong_data:
raise TypeError(
error_string(
wrong_data,
'neighbor'))
case wrong_data:
raise TypeError(error_string(
country,
'country elements'))
countries[name] = dict(
rank=rank,
year=year,
gdppc=gdppc,
neighbors=neighbors,
)
case wrong_data:
raise TypeError(error_string(wrong_data,
'country'))
return countries
###
if __name__ == '__main__':
import pprint
tree = tokenize_xml(COUNTRY_DATA)
countries = parse_countries_2(tree)
pprint.pprint(countries)