comparison env/lib/python3.7/site-packages/humanfriendly/compat.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 # Human friendly input/output in Python.
2 #
3 # Author: Peter Odding <peter@peterodding.com>
4 # Last Change: March 1, 2020
5 # URL: https://humanfriendly.readthedocs.io
6
7 """
8 Compatibility with Python 2 and 3.
9
10 This module exposes aliases and functions that make it easier to write Python
11 code that is compatible with Python 2 and Python 3.
12
13 .. data:: basestring
14
15 Alias for :func:`python2:basestring` (in Python 2) or :class:`python3:str`
16 (in Python 3). See also :func:`is_string()`.
17
18 .. data:: HTMLParser
19
20 Alias for :class:`python2:HTMLParser.HTMLParser` (in Python 2) or
21 :class:`python3:html.parser.HTMLParser` (in Python 3).
22
23 .. data:: interactive_prompt
24
25 Alias for :func:`python2:raw_input()` (in Python 2) or
26 :func:`python3:input()` (in Python 3).
27
28 .. data:: StringIO
29
30 Alias for :class:`python2:StringIO.StringIO` (in Python 2) or
31 :class:`python3:io.StringIO` (in Python 3).
32
33 .. data:: unicode
34
35 Alias for :func:`python2:unicode` (in Python 2) or :class:`python3:str` (in
36 Python 3). See also :func:`coerce_string()`.
37
38 .. data:: monotonic
39
40 Alias for :func:`python3:time.monotonic()` (in Python 3.3 and higher) or
41 `monotonic.monotonic()` (a `conditional dependency
42 <https://pypi.org/project/monotonic/>`_ on older Python versions).
43 """
44
45 __all__ = (
46 'HTMLParser',
47 'StringIO',
48 'basestring',
49 'coerce_string',
50 'interactive_prompt',
51 'is_string',
52 'is_unicode',
53 'monotonic',
54 'name2codepoint',
55 'on_windows',
56 'unichr',
57 'unicode',
58 # This export remains here so as not to break my dozen or so other Python
59 # projects using 'from humanfriendly.compat import unittest' from good old
60 # times (when Python 2.6 was still a thing). It will eventually be removed.
61 'unittest',
62 'which',
63 )
64
65 # Standard library modules.
66 import sys
67 import unittest
68
69 # Differences between Python 2 and 3.
70 try:
71 # Python 2.
72 unicode = unicode
73 unichr = unichr
74 basestring = basestring
75 interactive_prompt = raw_input
76 from distutils.spawn import find_executable as which
77 from HTMLParser import HTMLParser
78 from StringIO import StringIO
79 from htmlentitydefs import name2codepoint
80 except (ImportError, NameError):
81 # Python 3.
82 unicode = str
83 unichr = chr
84 basestring = str
85 interactive_prompt = input
86 from shutil import which
87 from html.parser import HTMLParser
88 from io import StringIO
89 from html.entities import name2codepoint
90
91 try:
92 # Python 3.3 and higher.
93 from time import monotonic
94 except ImportError:
95 # A replacement for older Python versions:
96 # https://pypi.org/project/monotonic/
97 try:
98 from monotonic import monotonic
99 except (ImportError, RuntimeError):
100 # We fall back to the old behavior of using time.time() instead of
101 # failing when {time,monotonic}.monotonic() are both missing.
102 from time import time as monotonic
103
104
105 def coerce_string(value):
106 """
107 Coerce any value to a Unicode string (:func:`python2:unicode` in Python 2 and :class:`python3:str` in Python 3).
108
109 :param value: The value to coerce.
110 :returns: The value coerced to a Unicode string.
111 """
112 return value if is_string(value) else unicode(value)
113
114
115 def is_string(value):
116 """
117 Check if a value is a :func:`python2:basestring` (in Python 2) or :class:`python3:str` (in Python 3) object.
118
119 :param value: The value to check.
120 :returns: :data:`True` if the value is a string, :data:`False` otherwise.
121 """
122 return isinstance(value, basestring)
123
124
125 def is_unicode(value):
126 """
127 Check if a value is a :func:`python2:unicode` (in Python 2) or :class:`python2:str` (in Python 3) object.
128
129 :param value: The value to check.
130 :returns: :data:`True` if the value is a Unicode string, :data:`False` otherwise.
131 """
132 return isinstance(value, unicode)
133
134
135 def on_windows():
136 """
137 Check if we're running on the Microsoft Windows OS.
138
139 :returns: :data:`True` if running Windows, :data:`False` otherwise.
140 """
141 return sys.platform.startswith('win')