forked from cloudant/python-cloudant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2to3.py
More file actions
107 lines (86 loc) · 3.56 KB
/
Copy path_2to3.py
File metadata and controls
107 lines (86 loc) · 3.56 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
# Copyright (c) 2016, 2017 IBM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Python 2 to 3 compatibility methods
The philosophy employed here is to treat py2 as the special case vs. py3 as
future Python releases presumably will retain new semamtics in py3.
"""
import sys
PY2 = sys.version_info[0] < 3
ENCODING = 'utf-8'
NONETYPE = type(None)
# pylint: disable=undefined-variable
STRTYPE = basestring if PY2 else str
# pylint: disable=undefined-variable
UNITYPE = unicode if PY2 else str
# pylint: disable=undefined-variable
LONGTYPE = long if PY2 else int
# pylint: disable=undefined-variable
UNICHR = unichr if PY2 else chr
if PY2:
# pylint: disable=wrong-import-position,no-name-in-module,import-error,unused-import
from urllib import quote as url_quote, quote_plus as url_quote_plus
from urlparse import urlparse as url_parse
from urlparse import urljoin as url_join
from ConfigParser import RawConfigParser
from cookielib import Cookie
def iteritems_(adict):
"""
iterate dict key, value tuples in a py2 and 3 compatible way
:param dict adict:
:return: iterator of (key, value) tuples
"""
return adict.iteritems()
def next_(itr):
"""
return next item from an iterable is a py2 and 3 compatible way
:param Iterable itr:
:return: the next item in itr
"""
return itr.next()
else:
from urllib.parse import urlparse as url_parse # pylint: disable=wrong-import-position,no-name-in-module,import-error,ungrouped-imports
from urllib.parse import urljoin as url_join # pylint: disable=wrong-import-position,no-name-in-module,import-error,ungrouped-imports
from urllib.parse import quote as url_quote # pylint: disable=wrong-import-position,no-name-in-module,import-error,ungrouped-imports
from urllib.parse import quote_plus as url_quote_plus # pylint: disable=wrong-import-position,no-name-in-module,import-error,ungrouped-imports
from configparser import RawConfigParser # pylint: disable=wrong-import-position,no-name-in-module,import-error
from http.cookiejar import Cookie # pylint: disable=wrong-import-position,no-name-in-module,import-error
def iteritems_(adict):
"""
iterate dict key, value tuples in a py2 and 3 compatible way
:param dict adict:
:return: iterator of (key, value) tuples
"""
return adict.items()
def next_(itr):
"""
return the next item in an iterable in a py2 and 3 compatible way
:param Iterable itr:
:return: the next item in itr
"""
return next(itr)
def bytes_(astr):
"""
return a bytes representation of astr in a py2 and 3 compatible way
:param str astr:
:return: bytes object
"""
return astr.encode(ENCODING) if hasattr(astr, 'encode') else astr
def unicode_(astr):
"""
return a unicode string representation of astr in a py2 and 3 compatible way
:param bytes astr:
:return: unicode string
"""
return astr.decode(ENCODING) if hasattr(astr, 'decode') else astr