Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/humanfriendly/compat.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 # Human friendly input/output in Python. | |
| 2 # | |
| 3 # Author: Peter Odding <peter@peterodding.com> | |
| 4 # Last Change: December 10, 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_macos', | |
| 56 'on_windows', | |
| 57 'unichr', | |
| 58 'unicode', | |
| 59 # This export remains here so as not to break my dozen or so other Python | |
| 60 # projects using 'from humanfriendly.compat import unittest' from good old | |
| 61 # times (when Python 2.6 was still a thing). It will eventually be removed. | |
| 62 'unittest', | |
| 63 'which', | |
| 64 ) | |
| 65 | |
| 66 # Standard library modules. | |
| 67 import sys | |
| 68 import unittest | |
| 69 | |
| 70 # Differences between Python 2 and 3. | |
| 71 try: | |
| 72 # Python 2. | |
| 73 unicode = unicode | |
| 74 unichr = unichr | |
| 75 basestring = basestring | |
| 76 interactive_prompt = raw_input | |
| 77 from distutils.spawn import find_executable as which | |
| 78 from HTMLParser import HTMLParser | |
| 79 from StringIO import StringIO | |
| 80 from htmlentitydefs import name2codepoint | |
| 81 except (ImportError, NameError): | |
| 82 # Python 3. | |
| 83 unicode = str | |
| 84 unichr = chr | |
| 85 basestring = str | |
| 86 interactive_prompt = input | |
| 87 from shutil import which | |
| 88 from html.parser import HTMLParser | |
| 89 from io import StringIO | |
| 90 from html.entities import name2codepoint | |
| 91 | |
| 92 try: | |
| 93 # Python 3.3 and higher. | |
| 94 from time import monotonic | |
| 95 except ImportError: | |
| 96 # A replacement for older Python versions: | |
| 97 # https://pypi.org/project/monotonic/ | |
| 98 try: | |
| 99 from monotonic import monotonic | |
| 100 except (ImportError, RuntimeError): | |
| 101 # We fall back to the old behavior of using time.time() instead of | |
| 102 # failing when {time,monotonic}.monotonic() are both missing. | |
| 103 from time import time as monotonic | |
| 104 | |
| 105 | |
| 106 def coerce_string(value): | |
| 107 """ | |
| 108 Coerce any value to a Unicode string (:func:`python2:unicode` in Python 2 and :class:`python3:str` in Python 3). | |
| 109 | |
| 110 :param value: The value to coerce. | |
| 111 :returns: The value coerced to a Unicode string. | |
| 112 """ | |
| 113 return value if is_string(value) else unicode(value) | |
| 114 | |
| 115 | |
| 116 def is_string(value): | |
| 117 """ | |
| 118 Check if a value is a :func:`python2:basestring` (in Python 2) or :class:`python3:str` (in Python 3) object. | |
| 119 | |
| 120 :param value: The value to check. | |
| 121 :returns: :data:`True` if the value is a string, :data:`False` otherwise. | |
| 122 """ | |
| 123 return isinstance(value, basestring) | |
| 124 | |
| 125 | |
| 126 def is_unicode(value): | |
| 127 """ | |
| 128 Check if a value is a :func:`python2:unicode` (in Python 2) or :class:`python2:str` (in Python 3) object. | |
| 129 | |
| 130 :param value: The value to check. | |
| 131 :returns: :data:`True` if the value is a Unicode string, :data:`False` otherwise. | |
| 132 """ | |
| 133 return isinstance(value, unicode) | |
| 134 | |
| 135 | |
| 136 def on_macos(): | |
| 137 """ | |
| 138 Check if we're running on Apple MacOS. | |
| 139 | |
| 140 :returns: :data:`True` if running MacOS, :data:`False` otherwise. | |
| 141 """ | |
| 142 return sys.platform.startswith('darwin') | |
| 143 | |
| 144 | |
| 145 def on_windows(): | |
| 146 """ | |
| 147 Check if we're running on the Microsoft Windows OS. | |
| 148 | |
| 149 :returns: :data:`True` if running Windows, :data:`False` otherwise. | |
| 150 """ | |
| 151 return sys.platform.startswith('win') |
