Fix str and bytes problem Fix sorted problem Fix math problem Fix CI problemrevert-12469-sum_op_dim_fix
parent
1f618c4ff9
commit
6abe819f07
@ -0,0 +1,74 @@
|
|||||||
|
# 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 six
|
||||||
|
|
||||||
|
|
||||||
|
# str and bytes related functions
|
||||||
|
def to_literal_str(obj):
|
||||||
|
if isinstance(obj, list):
|
||||||
|
return [_to_literal_str(item) for item in obj]
|
||||||
|
elif isinstance(obj, set):
|
||||||
|
return set([_to_literal_str(item) for item in obj])
|
||||||
|
else:
|
||||||
|
return _to_literal_str(obj)
|
||||||
|
|
||||||
|
|
||||||
|
def _to_literal_str(obj):
|
||||||
|
if isinstance(obj, six.binary_type):
|
||||||
|
return obj.decode('latin-1')
|
||||||
|
elif isinstance(obj, six.text_type):
|
||||||
|
return obj
|
||||||
|
else:
|
||||||
|
return six.u(obj)
|
||||||
|
|
||||||
|
|
||||||
|
def to_bytes(obj):
|
||||||
|
if isinstance(obj, list):
|
||||||
|
return [_to_bytes(item) for item in obj]
|
||||||
|
elif isinstance(obj, set):
|
||||||
|
return set([_to_bytes(item) for item in obj])
|
||||||
|
else:
|
||||||
|
return _to_bytes(obj)
|
||||||
|
|
||||||
|
|
||||||
|
def _to_bytes(obj):
|
||||||
|
if isinstance(obj, six.text_type):
|
||||||
|
return obj.encode('latin-1')
|
||||||
|
elif isinstance(obj, six.binary_type):
|
||||||
|
return obj
|
||||||
|
else:
|
||||||
|
return six.b(obj)
|
||||||
|
|
||||||
|
|
||||||
|
# math related functions
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def 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
|
||||||
|
"""
|
||||||
|
p = 10**d
|
||||||
|
return float(math.floor((x * p) + math.copysign(0.5, x))) / p
|
||||||
|
|
||||||
|
|
||||||
|
def floor_division(x, y):
|
||||||
|
return x // y
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue