-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
59 lines (44 loc) · 1.35 KB
/
util.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
"""
Utils.
"""
import logging
from geopy.compat import py3k, text_type
if not py3k: # pragma: no cover
NUMBER_TYPES = (int, long, float) # noqa
else: # pragma: no cover
NUMBER_TYPES = (int, float) # long -> int in Py3k
try:
from decimal import Decimal
NUMBER_TYPES = NUMBER_TYPES + (Decimal, )
except ImportError: # pragma: no cover
pass
__version__ = "1.20.0"
logger = logging.getLogger('geopy')
def pairwise(seq):
"""
Pair an iterable, e.g., (1, 2, 3, 4) -> ((1, 2), (2, 3), (3, 4))
"""
for i in range(0, len(seq) - 1):
yield (seq[i], seq[i + 1])
def join_filter(sep, seq, pred=bool):
"""
Join with a filter.
"""
return sep.join([text_type(i) for i in seq if pred(i)])
def decode_page(page):
"""
Return unicode string of geocoder results.
Nearly all services use JSON, so assume UTF8 encoding unless the
response specifies otherwise.
"""
if hasattr(page, 'read'): # urllib
if py3k:
encoding = page.headers.get_param("charset") or "utf-8"
else:
encoding = page.headers.getparam("charset") or "utf-8"
return text_type(page.read(), encoding=encoding)
else: # requests?
encoding = page.headers.get("charset") or "utf-8"
return text_type(page.content, encoding=encoding)
def get_version():
return __version__