forked from scanny/python-pptx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython2.py
More file actions
48 lines (36 loc) · 1.21 KB
/
python2.py
File metadata and controls
48 lines (36 loc) · 1.21 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
# encoding: utf-8
"""
Provides Python 2 compatibility objects
"""
from StringIO import StringIO as BytesIO # noqa
def is_integer(obj):
"""
Return True if *obj* is an integer (int, long), False otherwise.
"""
return isinstance(obj, (int, long))
def is_string(obj):
"""
Return True if *obj* is a string, False otherwise.
"""
return isinstance(obj, basestring)
def is_unicode(obj):
"""
Return True if *obj* is a unicode string, False otherwise.
"""
return isinstance(obj, unicode)
def to_unicode(text):
"""
Return *text* as a unicode string. *text* can be a 7-bit ASCII string,
a UTF-8 encoded 8-bit string, or unicode. String values are converted to
unicode assuming UTF-8 encoding. Unicode values are returned unchanged.
"""
# both str and unicode inherit from basestring
if not isinstance(text, basestring):
tmpl = 'expected UTF-8 encoded string or unicode, got %s value %s'
raise TypeError(tmpl % (type(text), text))
# return unicode strings unchanged
if isinstance(text, unicode):
return text
# otherwise assume UTF-8 encoding, which also works for ASCII
return unicode(text, 'utf-8')
Unicode = unicode