Skip to content

Commit 4274297

Browse files
committed
functional programming parse xml
1 parent 419a34c commit 4274297

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

functional_program/functional_use_set/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import ssl
2+
import urllib.request
3+
import xml.etree.ElementTree as XML
4+
from typing import Text, List, TextIO, Iterable, Tuple
5+
6+
ssl._create_default_https_context = ssl._create_unverified_context
7+
8+
9+
def row_iter_kml(file_obj: TextIO) -> Iterable[List]:
10+
ns_map = {
11+
'ns0': 'http://www.opengis.net/kml/2.2',
12+
'ns1': 'http://www.google.com/kml/ext/2.2'
13+
}
14+
path_to_points = ('./ns0:Placemark/'
15+
'ns0:Point/ns0:coordinates')
16+
doc = XML.parse(file_obj)
17+
return (comma_split(Text(coordinates.text))
18+
for coordinates in
19+
doc.findall(path_to_points, ns_map))
20+
21+
22+
def comma_split(text: Text) -> List[Text]:
23+
return text.split(',')
24+
25+
26+
def pick_lat_lon(lon: Text, lat: Text, alt: Text) -> Tuple[Text, Text]:
27+
return lat, lon
28+
29+
30+
Rows = Iterable[List[Text]]
31+
LL_Text = Tuple[Text, Text]
32+
33+
34+
def lat_lon_kml(row_iter: Rows) -> Iterable[LL_Text]:
35+
return (pick_lat_lon(*row) for row in row_iter)
36+
37+
38+
url = 'https://raw.githubusercontent.com/googlearchive/kml-samples/gh-pages/kml/Placemark/placemark.kml'
39+
with urllib.request.urlopen(url) as source:
40+
v1 = tuple(row_iter_kml(source))
41+
print(v1)
42+
43+
44+
45+

0 commit comments

Comments
 (0)