forked from geopy/geopy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.py
More file actions
101 lines (90 loc) · 3.2 KB
/
Copy pathcompat.py
File metadata and controls
101 lines (90 loc) · 3.2 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
"""
Compatibility...
"""
import sys
py3k = sys.version_info >= (3, 0)
if py3k: # pragma: no cover
string_compare = str
else: # pragma: no cover
string_compare = (str, unicode)
# Unicode compatibility, borrowed from 'six'
if py3k: # pragma: no cover
def u(s):
"""
Convert to Unicode with py3k
"""
return s
else: # pragma: no cover
def u(s):
"""
Convert to Unicode with unicode escaping
"""
return unicode(s.replace(r'\\', r'\\\\'), 'unicode_escape')
if py3k: # pragma: no cover
from urllib.parse import (urlencode, quote, # pylint: disable=W0611,F0401,W0611,E0611
urlparse, parse_qs)
from urllib.request import (Request, urlopen, # pylint: disable=W0611,F0401,W0611,E0611
build_opener, ProxyHandler,
URLError, install_opener,
HTTPPasswordMgrWithDefaultRealm,
HTTPBasicAuthHandler)
from urllib.error import HTTPError # pylint: disable=W0611,F0401,W0611,E0611
def itervalues(d):
"""
Function for iterating on values due to methods
renaming between Python 2 and 3 versions
For Python2
"""
return iter(d.values())
def iteritems(d):
"""
Function for iterating on items due to methods
renaming between Python 2 and 3 versions
For Python2
"""
return iter(d.items())
else: # pragma: no cover
from urllib import urlencode as original_urlencode, quote # pylint: disable=W0611,F0401,W0611,E0611
from urllib2 import (Request, HTTPError, # pylint: disable=W0611,F0401,W0611,E0611
ProxyHandler, URLError, urlopen,
build_opener, install_opener,
HTTPPasswordMgrWithDefaultRealm,
HTTPBasicAuthHandler)
from urlparse import urlparse, parse_qs
def force_str(str_or_unicode):
"""
Python2-only, ensures that a string is encoding to a str.
"""
if isinstance(str_or_unicode, unicode):
return str_or_unicode.encode('utf-8')
else:
return str_or_unicode
def urlencode(query, doseq=0):
"""
A version of Python's urllib.urlencode() function that can operate on
unicode strings. The parameters are first cast to UTF-8 encoded strings
and then encoded as per normal.
Based on the urlencode from django.utils.http
"""
if hasattr(query, 'items'):
query = query.items()
return original_urlencode(
[(force_str(k),
[force_str(i) for i in v]
if isinstance(v, (list, tuple)) else force_str(v))
for k, v in query],
doseq)
def itervalues(d):
"""
Function for iterating on values due to methods
renaming between Python 2 and 3 versions
For Python3
"""
return d.itervalues()
def iteritems(d):
"""
Function for iterating on items due to methods
renaming between Python 2 and 3 versions
For Python3
"""
return d.iteritems()