同步操作将从 PaddlePaddle/Paddle 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# Copyright (c) 2018 PaddlePaddle Authors. 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.import siximport math__all__ = []int_type = intlong_type = int# str and bytes related functionsdef to_text(obj, encoding='utf-8', inplace=False):"""All string in PaddlePaddle should be represented as a literal string.This function will convert object to a literal string without any encoding.Especially, if the object type is a list or set container, we will iterateall items in the object and convert them to literal string.In Python3:Decode the bytes type object to str type with specific encodingIn Python2:Decode the str type object to unicode type with specific encodingArgs:obj(unicode|str|bytes|list|set) : The object to be decoded.encoding(str) : The encoding format to decode a stringinplace(bool) : If we change the original object or we create a new oneReturns:Decoded result of objExamples:.. code-block:: pythonimport paddledata = "paddlepaddle"data = paddle.compat.to_text(data)# paddlepaddle"""if obj is None:return objif isinstance(obj, list):if inplace:for i in six.moves.xrange(len(obj)):obj[i] = _to_text(obj[i], encoding)return objelse:return [_to_text(item, encoding) for item in obj]elif isinstance(obj, set):if inplace:for item in obj:obj.remove(item)obj.add(_to_text(item, encoding))return objelse:return set([_to_text(item, encoding) for item in obj])elif isinstance(obj, dict):if inplace:new_obj = {}for key, value in six.iteritems(obj):new_obj[_to_text(key, encoding)] = _to_text(value, encoding)obj.update(new_obj)return objelse:new_obj = {}for key, value in six.iteritems(obj):new_obj[_to_text(key, encoding)] = _to_text(value, encoding)return new_objelse:return _to_text(obj, encoding)def _to_text(obj, encoding):"""In Python3:Decode the bytes type object to str type with specific encodingIn Python2:Decode the str type object to unicode type with specific encoding,or we just return the unicode string of objectArgs:obj(unicode|str|bytes) : The object to be decoded.encoding(str) : The encoding formatReturns:decoded result of obj"""if obj is None:return objif isinstance(obj, six.binary_type):return obj.decode(encoding)elif isinstance(obj, six.text_type):return objelif isinstance(obj, (bool, float)):return objelse:return six.u(obj)def to_bytes(obj, encoding='utf-8', inplace=False):"""All string in PaddlePaddle should be represented as a literal string.This function will convert object to a bytes with specific encoding.Especially, if the object type is a list or set container, we will iterateall items in the object and convert them to bytes.In Python3:Encode the str type object to bytes type with specific encodingIn Python2:Encode the unicode type object to str type with specific encoding,or we just return the 8-bit string of objectArgs:obj(unicode|str|bytes|list|set) : The object to be encoded.encoding(str) : The encoding format to encode a stringinplace(bool) : If we change the original object or we create a new oneReturns:Decoded result of objExamples:.. code-block:: pythonimport paddledata = "paddlepaddle"data = paddle.compat.to_bytes(data)# b'paddlepaddle'"""if obj is None:return objif isinstance(obj, list):if inplace:for i in six.moves.xrange(len(obj)):obj[i] = _to_bytes(obj[i], encoding)return objelse:return [_to_bytes(item, encoding) for item in obj]elif isinstance(obj, set):if inplace:for item in obj:obj.remove(item)obj.add(_to_bytes(item, encoding))return objelse:return set([_to_bytes(item, encoding) for item in obj])else:return _to_bytes(obj, encoding)def _to_bytes(obj, encoding):"""In Python3:Encode the str type object to bytes type with specific encodingIn Python2:Encode the unicode type object to str type with specific encoding,or we just return the 8-bit string of objectArgs:obj(unicode|str|bytes) : The object to be encoded.encoding(str) : The encoding formatReturns:encoded result of obj"""if obj is None:return objassert encoding is not Noneif isinstance(obj, six.text_type):return obj.encode(encoding)elif isinstance(obj, six.binary_type):return objelse:return six.b(obj)# math related functionsdef round(x, d=0):"""Compatible round which act the same behaviour in Python3.Args:x(float) : The number to round halfway.Returns:round result of x"""if six.PY3:# The official walkaround of round in Python3 is incorrect# we implement according this answer: https://www.techforgeek.info/round_python.htmlif x > 0.0:p = 10**dreturn float(math.floor((x * p) + math.copysign(0.5, x))) / pelif x < 0.0:p = 10**dreturn float(math.ceil((x * p) + math.copysign(0.5, x))) / pelse:return math.copysign(0.0, x)else:import __builtin__return __builtin__.round(x, d)def floor_division(x, y):"""Compatible division which act the same behaviour in Python3 and Python2,whose result will be a int value of floor(x / y) in Python3 and value of(x / y) in Python2.Args:x(int|float) : The number to divide.y(int|float) : The number to be dividedReturns:division result of x // y"""return x // y# exception related functionsdef get_exception_message(exc):"""Get the error message of a specific exceptionArgs:exec(Exception) : The exception to get error message.Returns:the error message of exec"""assert exc is not Nonereturn str(exc)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。