Skip to content

Commit f90d96f

Browse files
committed
functinal programming more compaction style
1 parent 4a39b07 commit f90d96f

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

functional_program/functional_use_set/functional_xml.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import ssl
22
import urllib.request
33
import xml.etree.ElementTree as XML
4-
from typing import Text, List, TextIO, Iterable, Tuple
4+
from typing import Text, List, TextIO, Iterable, Tuple, TypeVar, Iterator
55

66
ssl._create_default_https_context = ssl._create_unverified_context
77

88

99
def row_iter_kml(file_obj: TextIO) -> Iterable[List]:
1010
ns_map = {
1111
'ns0': 'http://www.opengis.net/kml/2.2',
12-
'ns1': 'http://www.google.com/kml/ext/2.2'
12+
'ns1': 'http://www.google.com/kml/ext/2.2',
13+
'ns2': 'http://earth.google.com/kml/2.1',
1314
}
14-
path_to_points = ('./ns0:Document/ns0:Placemark/'
15-
'ns0:Point/ns0:coordinates')
15+
path_to_points = ('./ns2:Document/ns2:Placemark/'
16+
'ns2:Point/ns2:coordinates')
1617
doc = XML.parse(file_obj)
1718
return (comma_split(Text(coordinates.text))
1819
for coordinates in
@@ -35,7 +36,29 @@ def lat_lon_kml(row_iter: Rows) -> Iterable[LL_Text]:
3536
return (pick_lat_lon(*row) for row in row_iter)
3637

3738

38-
url = 'https://raw.githubusercontent.com/googlearchive/kml-samples/gh-pages/kml/Placemark/Point/altitude.kml'
39+
T_ = TypeVar('T_')
40+
Pairs_iter = Iterator[Tuple[T_, T_]]
41+
42+
43+
def legs(lat_lon_iter: Iterator[T_]) -> Pairs_iter:
44+
begin = next(lat_lon_iter)
45+
for end in lat_lon_iter:
46+
yield begin, end
47+
begin = end
48+
49+
50+
Text_Iter = Iterable[Tuple[Text, Text]]
51+
LL_Iter = Iterator[Tuple[float, float]]
52+
53+
54+
def float_from_pair(lat_lon_iter: Text_Iter) -> LL_Iter:
55+
return (
56+
(float(lat), float(lon))
57+
for lat, lon in lat_lon_iter
58+
)
59+
60+
61+
url = 'https://raw.githubusercontent.com/googlearchive/kml-samples/gh-pages/kml_handbook/mvdoqq.kml'
3962
with urllib.request.urlopen(url) as source:
4063
v1 = tuple(row_iter_kml(source))
4164
print(v1)
@@ -45,3 +68,20 @@ def lat_lon_kml(row_iter: Rows) -> Iterable[LL_Text]:
4568
v2 = tuple(lat_lon_kml(row_iter_kml(source)))
4669
print(v2)
4770

71+
72+
with urllib.request.urlopen(url) as source:
73+
flt = ((float(lat), float(lon)) for lat, lon in lat_lon_kml(row_iter_kml(source)))
74+
for item in legs(flt):
75+
print(item)
76+
77+
print(tuple(flt))
78+
79+
80+
with urllib.request.urlopen(url) as source:
81+
v3 = legs(float_from_pair(
82+
lat_lon_kml(
83+
row_iter_kml(source)
84+
)))
85+
print(tuple(v3))
86+
87+

0 commit comments

Comments
 (0)