Repository navigation
Expand file tree
/
Copy pathch04_ex2.py
More file actions
152 lines (118 loc) · 5.06 KB
/
Copy pathch04_ex2.py
File metadata and controls
152 lines (118 loc) · 5.06 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
#!/usr/bin/env python3
"""Functional Python Programming
Chapter 4, Example Set 2
Also used in Chapter 5.
"""
from Chapter_4.ch04_ex1 import float_from_pair, float_lat_lon, row_iter_kml, limits, haversine, legs
import urllib.request
def wrap(leg_iter):
return ( (leg[2],leg) for leg in leg_iter )
def unwrap(dist_leg):
distance, leg = dist_leg
return leg
def by_dist(leg):
lat, lon, dist= leg
return dist
test_max_alternatives= """
>>> with urllib.request.urlopen("file:./Winter%202012-2013.kml") as source:
... path= float_from_pair(float_lat_lon(row_iter_kml(source)))
... trip= tuple( (start, end, round(haversine(start, end),4))
... for start,end in legs(path))
>>> long, short = max(dist for start,end,dist in trip), min(dist for start,end,dist in trip)
>>> long
129.7748
>>> short
0.1731
>>> long, short = unwrap( max( wrap( trip ) ) ), unwrap( min( wrap( trip ) ) )
>>> long
((27.154167, -80.195663), (29.195168, -81.002998), 129.7748)
>>> short
((35.505665, -76.653664), (35.508335, -76.654999), 0.1731)
>>> long, short = max(trip, key=by_dist), min(trip, key=by_dist)
>>> long
((27.154167, -80.195663), (29.195168, -81.002998), 129.7748)
>>> short
((35.505665, -76.653664), (35.508335, -76.654999), 0.1731)
"""
start= lambda x: x[0]
end = lambda x: x[1]
dist = lambda x: x[2]
lat = lambda x: x[0]
lon = lambda x: x[1]
test_min_max= """
>>> with urllib.request.urlopen("file:./Winter%202012-2013.kml") as source:
... path= float_from_pair(float_lat_lon(row_iter_kml(source)))
... trip= tuple( (start, end, round(haversine(start, end),4))
... for start,end in legs(path))
>>> long, short = max(trip, key=dist), min(trip, key=dist)
>>> long
((27.154167, -80.195663), (29.195168, -81.002998), 129.7748)
>>> short
((35.505665, -76.653664), (35.508335, -76.654999), 0.1731)
>>> north = min( trip, key=lambda x: lat(start(x)) )
>>> north
((23.9555, -76.31633), (24.099667, -76.401833), 9.844)
"""
test_conversion="""
>>> with urllib.request.urlopen("file:./Winter%202012-2013.kml") as source:
... path= float_from_pair(float_lat_lon(row_iter_kml(source)))
... trip= tuple( (start, end, round(haversine(start, end),4))
... for start,end in legs(path))
>>> statute1 = list( (start(x),end(x),dist(x)*6076.12/5280) for x in trip )
>>> statute2 = list( map( lambda x: (start(x),end(x),dist(x)*6076.12/5280), trip ) )
>>> statute3 = list( (b, e, d*6076.12/5280) for b, e, d in trip )
>>> assert statute1 == statute2
>>> assert statute1 == statute3
>>> statute1[0]
((37.54901619777347, -76.33029518659048), (37.840832, -76.273834), 20.397120559090908)
>>> statute1[-1]
((38.330166, -76.458504), (38.976334, -76.473503), 44.652462240151515)
"""
test_filter_sorted="""
>>> with urllib.request.urlopen("file:./Winter%202012-2013.kml") as source:
... path= float_from_pair(float_lat_lon(row_iter_kml(source)))
... trip= tuple( (start, end, round(haversine(start, end),4))
... for start,end in legs(path))
>>> long= list(filter( lambda leg: dist(leg) >= 50, trip ))
>>> len(long)
14
>>> long[0]
((34.204666, -77.800499), (33.276833, -78.979332), 81.0363)
>>> long[-1]
((31.9105, -80.780998), (32.83248254681784, -79.93379468285697), 70.0694)
>>> s1= sorted( dist(x) for x in trip)
>>> s1[0]
0.1731
>>> s1[-1]
129.7748
>>> s2=( sorted( trip, key=dist ) )
>>> s2[0]
((35.505665, -76.653664), (35.508335, -76.654999), 0.1731)
>>> s2[-1]
((27.154167, -80.195663), (29.195168, -81.002998), 129.7748)
>>> from Chapter_4.ch04_ex4 import mean, stdev, z
>>> dist_data = list(map(dist, trip))
>>> μ_d = mean(dist_data)
>>> σ_d = stdev(dist_data)
>>> print( "Average leg", μ_d, "with σ_d of", σ_d, "Z(0)=", z(0,μ_d,σ_d) )
Average leg 33.99131780821918 with σ_d of 24.158473730346035 Z(0)= -1.407014291864054
>>> outlier = lambda leg: abs(z(dist(leg),μ_d,σ_d)) > 3
>>> print( "Outliers", list( filter( outlier, trip ) ) )
Outliers [((29.050501, -80.651169), (27.186001, -80.139503), 115.1751), ((27.154167, -80.195663), (29.195168, -81.002998), 129.7748)]
"""
def performance():
import timeit
print( "map", timeit.timeit( """list(map(int,data))""", """data= ['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41', '43', '47', '53', '59', '61', '67', '71', '73', '79', '83', '89', '97', '101', '103', '107', '109', '113', '127', '131', '137', '139', '149', '151', '157', '163', '167', '173', '179', '181', '191', '193', '197', '199', '211', '223', '227', '229']""" ) )
print( "expr", timeit.timeit( """list(int(v) for v in data)""", """data= ['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37', '41', '43', '47', '53', '59', '61', '67', '71', '73', '79', '83', '89', '97', '101', '103', '107', '109', '113', '127', '131', '137', '139', '149', '151', '157', '163', '167', '173', '179', '181', '191', '193', '197', '199', '211', '223', '227', '229']
""") )
__test__ = {
"test_max_alternatives": test_max_alternatives,
"test_min_max": test_min_max,
"test_conversion": test_conversion,
"test_filter_sorted": test_filter_sorted,
}
def test():
import doctest
doctest.testmod(verbose=1)
if __name__ == "__main__":
test()