|
| 1 | +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import six |
| 16 | +import math |
| 17 | + |
| 18 | +__all__ = [ |
| 19 | + 'long_type', |
| 20 | + 'to_text', |
| 21 | + 'to_bytes', |
| 22 | + 'round', |
| 23 | + 'floor_division', |
| 24 | + 'get_exception_message', |
| 25 | +] |
| 26 | + |
| 27 | +if six.PY2: |
| 28 | + int_type = int |
| 29 | + long_type = long |
| 30 | +else: |
| 31 | + int_type = int |
| 32 | + long_type = int |
| 33 | + |
| 34 | + |
| 35 | +# str and bytes related functions |
| 36 | +def to_text(obj, encoding='utf-8', inplace=False): |
| 37 | + """ |
| 38 | + All string in PaddlePaddle should be represented as a literal string. |
| 39 | + This function will convert object to a literal string without any encoding. |
| 40 | + Especially, if the object type is a list or set container, we will iterate |
| 41 | + all items in the object and convert them to literal string. |
| 42 | +
|
| 43 | + In Python3: |
| 44 | + Decode the bytes type object to str type with specific encoding |
| 45 | +
|
| 46 | + In Python2: |
| 47 | + Decode the str type object to unicode type with specific encoding |
| 48 | +
|
| 49 | + Args: |
| 50 | + obj(unicode|str|bytes|list|set) : The object to be decoded. |
| 51 | + encoding(str) : The encoding format to decode a string |
| 52 | + inplace(bool) : If we change the original object or we create a new one |
| 53 | +
|
| 54 | + Returns: |
| 55 | + Decoded result of obj |
| 56 | + """ |
| 57 | + if obj is None: |
| 58 | + return obj |
| 59 | + |
| 60 | + if isinstance(obj, list): |
| 61 | + if inplace: |
| 62 | + for i in six.moves.xrange(len(obj)): |
| 63 | + obj[i] = _to_text(obj[i], encoding) |
| 64 | + return obj |
| 65 | + else: |
| 66 | + return [_to_text(item, encoding) for item in obj] |
| 67 | + elif isinstance(obj, set): |
| 68 | + if inplace: |
| 69 | + for item in obj: |
| 70 | + obj.remove(item) |
| 71 | + obj.add(_to_text(item, encoding)) |
| 72 | + return obj |
| 73 | + else: |
| 74 | + return set([_to_text(item, encoding) for item in obj]) |
| 75 | + else: |
| 76 | + return _to_text(obj, encoding) |
| 77 | + |
| 78 | + |
| 79 | +def _to_text(obj, encoding): |
| 80 | + """ |
| 81 | + In Python3: |
| 82 | + Decode the bytes type object to str type with specific encoding |
| 83 | +
|
| 84 | + In Python2: |
| 85 | + Decode the str type object to unicode type with specific encoding, |
| 86 | + or we just return the unicode string of object |
| 87 | +
|
| 88 | + Args: |
| 89 | + obj(unicode|str|bytes) : The object to be decoded. |
| 90 | + encoding(str) : The encoding format |
| 91 | +
|
| 92 | + Returns: |
| 93 | + decoded result of obj |
| 94 | + """ |
| 95 | + if obj is None: |
| 96 | + return obj |
| 97 | + |
| 98 | + if isinstance(obj, six.binary_type): |
| 99 | + return obj.decode(encoding) |
| 100 | + elif isinstance(obj, six.text_type): |
| 101 | + return obj |
| 102 | + else: |
| 103 | + return six.u(obj) |
| 104 | + |
| 105 | + |
| 106 | +def to_bytes(obj, encoding='utf-8', inplace=False): |
| 107 | + """ |
| 108 | + All string in PaddlePaddle should be represented as a literal string. |
| 109 | + This function will convert object to a bytes with specific encoding. |
| 110 | + Especially, if the object type is a list or set container, we will iterate |
| 111 | + all items in the object and convert them to bytes. |
| 112 | +
|
| 113 | + In Python3: |
| 114 | + Encode the str type object to bytes type with specific encoding |
| 115 | +
|
| 116 | + In Python2: |
| 117 | + Encode the unicode type object to str type with specific encoding, |
| 118 | + or we just return the 8-bit string of object |
| 119 | +
|
| 120 | + Args: |
| 121 | + obj(unicode|str|bytes|list|set) : The object to be encoded. |
| 122 | + encoding(str) : The encoding format to encode a string |
| 123 | + inplace(bool) : If we change the original object or we create a new one |
| 124 | +
|
| 125 | + Returns: |
| 126 | + Decoded result of obj |
| 127 | + """ |
| 128 | + if obj is None: |
| 129 | + return obj |
| 130 | + |
| 131 | + if isinstance(obj, list): |
| 132 | + if inplace: |
| 133 | + for i in six.moves.xrange(len(obj)): |
| 134 | + obj[i] = _to_bytes(obj[i], encoding) |
| 135 | + return obj |
| 136 | + else: |
| 137 | + return [_to_bytes(item, encoding) for item in obj] |
| 138 | + elif isinstance(obj, set): |
| 139 | + if inplace: |
| 140 | + for item in obj: |
| 141 | + obj.remove(item) |
| 142 | + obj.add(_to_bytes(item, encoding)) |
| 143 | + return obj |
| 144 | + else: |
| 145 | + return set([_to_bytes(item, encoding) for item in obj]) |
| 146 | + else: |
| 147 | + return _to_bytes(obj, encoding) |
| 148 | + |
| 149 | + |
| 150 | +def _to_bytes(obj, encoding): |
| 151 | + """ |
| 152 | + In Python3: |
| 153 | + Encode the str type object to bytes type with specific encoding |
| 154 | +
|
| 155 | + In Python2: |
| 156 | + Encode the unicode type object to str type with specific encoding, |
| 157 | + or we just return the 8-bit string of object |
| 158 | +
|
| 159 | + Args: |
| 160 | + obj(unicode|str|bytes) : The object to be encoded. |
| 161 | + encoding(str) : The encoding format |
| 162 | +
|
| 163 | + Returns: |
| 164 | + encoded result of obj |
| 165 | + """ |
| 166 | + if obj is None: |
| 167 | + return obj |
| 168 | + |
| 169 | + assert encoding is not None |
| 170 | + if isinstance(obj, six.text_type): |
| 171 | + return obj.encode(encoding) |
| 172 | + elif isinstance(obj, six.binary_type): |
| 173 | + return obj |
| 174 | + else: |
| 175 | + return six.b(obj) |
| 176 | + |
| 177 | + |
| 178 | +# math related functions |
| 179 | +def round(x, d=0): |
| 180 | + """ |
| 181 | + Compatible round which act the same behaviour in Python3. |
| 182 | +
|
| 183 | + Args: |
| 184 | + x(float) : The number to round halfway. |
| 185 | +
|
| 186 | + Returns: |
| 187 | + round result of x |
| 188 | + """ |
| 189 | + if six.PY3: |
| 190 | + # The official walkaround of round in Python3 is incorrect |
| 191 | + # we implement accroding this answer: https://www.techforgeek.info/round_python.html |
| 192 | + if x > 0.0: |
| 193 | + p = 10**d |
| 194 | + return float(math.floor((x * p) + math.copysign(0.5, x))) / p |
| 195 | + elif x < 0.0: |
| 196 | + p = 10**d |
| 197 | + return float(math.ceil((x * p) + math.copysign(0.5, x))) / p |
| 198 | + else: |
| 199 | + return math.copysign(0.0, x) |
| 200 | + else: |
| 201 | + import __builtin__ |
| 202 | + return __builtin__.round(x, d) |
| 203 | + |
| 204 | + |
| 205 | +def floor_division(x, y): |
| 206 | + """ |
| 207 | + Compatible division which act the same behaviour in Python3 and Python2, |
| 208 | + whose result will be a int value of floor(x / y) in Python3 and value of |
| 209 | + (x / y) in Python2. |
| 210 | +
|
| 211 | + Args: |
| 212 | + x(int|float) : The number to divide. |
| 213 | + y(int|float) : The number to be divided |
| 214 | +
|
| 215 | + Returns: |
| 216 | + division result of x // y |
| 217 | + """ |
| 218 | + return x // y |
| 219 | + |
| 220 | + |
| 221 | +# exception related functions |
| 222 | +def get_exception_message(exc): |
| 223 | + """ |
| 224 | + Get the error message of a specific exception |
| 225 | +
|
| 226 | + Args: |
| 227 | + exec(Exception) : The exception to get error message. |
| 228 | +
|
| 229 | + Returns: |
| 230 | + the error message of exec |
| 231 | + """ |
| 232 | + assert exc is not None |
| 233 | + |
| 234 | + if six.PY2: |
| 235 | + return exc.message |
| 236 | + else: |
| 237 | + return str(exc) |
0 commit comments