Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
09103084
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
09103084
编写于
8月 09, 2018
作者:
M
minqiyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish compat.py and add unittest for it
上级
c3fdf3ae
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
650 addition
and
12 deletion
+650
-12
paddle/fluid/framework/op_desc.cc
paddle/fluid/framework/op_desc.cc
+0
-1
python/paddle/fluid/compat.py
python/paddle/fluid/compat.py
+154
-8
python/paddle/fluid/layers/detection.py
python/paddle/fluid/layers/detection.py
+6
-3
python/paddle/fluid/tests/unittests/test_compat.py
python/paddle/fluid/tests/unittests/test_compat.py
+490
-0
未找到文件。
paddle/fluid/framework/op_desc.cc
浏览文件 @
09103084
...
...
@@ -202,7 +202,6 @@ std::vector<std::string> OpDesc::AttrNames() const {
}
void
OpDesc
::
SetAttr
(
const
std
::
string
&
name
,
const
Attribute
&
v
)
{
VLOG
(
11
)
<<
"SetAttr: "
<<
Type
()
<<
", "
<<
name
<<
", "
<<
v
.
which
();
// NOTICE(minqiyang): pybind11 will take the empty list in python as
// the std::vector<int> type in C++; so we have to change the attr's type
// here if we meet this issue
...
...
python/paddle/fluid/compat.py
浏览文件 @
09103084
...
...
@@ -15,18 +15,77 @@
import
six
import
math
__all__
=
[
'to_literal_str'
,
'to_bytes'
,
'round'
,
'floor_division'
,
'get_exception_message'
,
]
# str and bytes related functions
def
to_literal_str
(
obj
,
encoding
=
'utf-8'
):
def
to_literal_str
(
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 iterate
all items in the object and convert them to literal string.
In Python3:
Decode the bytes type object to str type with specific encoding
In Python2:
Decode the str type object to unicode type with specific encoding
Args:
obj(unicode|str|bytes|list|set) : The object to be decoded.
encoding(str) : The encoding format to decode a string
inplace(bool) : If we change the original object or we create a new one
Returns:
Decoded result of obj
"""
if
obj
is
None
:
return
obj
if
isinstance
(
obj
,
list
):
return
[
_to_literal_str
(
item
,
encoding
)
for
item
in
obj
]
if
inplace
:
for
i
in
six
.
moves
.
xrange
(
len
(
obj
)):
obj
[
i
]
=
_to_literal_str
(
obj
[
i
],
encoding
)
return
obj
else
:
return
[
_to_literal_str
(
item
,
encoding
)
for
item
in
obj
]
elif
isinstance
(
obj
,
set
):
return
set
([
_to_literal_str
(
item
,
encoding
)
for
item
in
obj
])
if
inplace
:
for
item
in
obj
:
obj
.
remove
(
item
)
obj
.
add
(
_to_literal_str
(
item
,
encoding
))
return
obj
else
:
return
set
([
_to_literal_str
(
item
,
encoding
)
for
item
in
obj
])
else
:
return
_to_literal_str
(
obj
,
encoding
)
def
_to_literal_str
(
obj
,
encoding
):
"""
In Python3:
Decode the bytes type object to str type with specific encoding
In Python2:
Decode the str type object to unicode type with specific encoding,
or we just return the unicode string of object
Args:
obj(unicode|str|bytes) : The object to be decoded.
encoding(str) : The encoding format
Returns:
decoded result of obj
"""
if
obj
is
None
:
return
obj
if
isinstance
(
obj
,
six
.
binary_type
):
return
obj
.
decode
(
encoding
)
elif
isinstance
(
obj
,
six
.
text_type
):
...
...
@@ -35,16 +94,70 @@ def _to_literal_str(obj, encoding):
return
six
.
u
(
obj
)
def
to_bytes
(
obj
,
encoding
=
'utf-8'
):
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 iterate
all items in the object and convert them to bytes.
In Python3:
Encode the str type object to bytes type with specific encoding
In Python2:
Encode the unicode type object to str type with specific encoding,
or we just return the 8-bit string of object
Args:
obj(unicode|str|bytes|list|set) : The object to be encoded.
encoding(str) : The encoding format to encode a string
inplace(bool) : If we change the original object or we create a new one
Returns:
Decoded result of obj
"""
if
obj
is
None
:
return
obj
if
isinstance
(
obj
,
list
):
return
[
_to_bytes
(
item
,
encoding
)
for
item
in
obj
]
if
inplace
:
for
i
in
six
.
moves
.
xrange
(
len
(
obj
)):
obj
[
i
]
=
_to_bytes
(
obj
[
i
],
encoding
)
return
obj
else
:
return
[
_to_bytes
(
item
,
encoding
)
for
item
in
obj
]
elif
isinstance
(
obj
,
set
):
return
set
([
_to_bytes
(
item
,
encoding
)
for
item
in
obj
])
if
inplace
:
for
item
in
obj
:
obj
.
remove
(
item
)
obj
.
add
(
_to_bytes
(
item
,
encoding
))
return
obj
else
:
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 encoding
In Python2:
Encode the unicode type object to str type with specific encoding,
or we just return the 8-bit string of object
Args:
obj(unicode|str|bytes) : The object to be encoded.
encoding(str) : The encoding format
Returns:
encoded result of obj
"""
if
obj
is
None
:
return
obj
assert
encoding
is
not
None
if
isinstance
(
obj
,
six
.
text_type
):
return
obj
.
encode
(
encoding
)
elif
isinstance
(
obj
,
six
.
binary_type
):
...
...
@@ -64,15 +177,48 @@ def round(x, d=0):
Returns:
round result of x
"""
p
=
10
**
d
return
float
(
math
.
floor
((
x
*
p
)
+
math
.
copysign
(
0.5
,
x
)))
/
p
if
six
.
PY3
:
# The official walkaround of round in Python3 is incorrect
# we implement accroding this answer: https://www.techforgeek.info/round_python.html
if
x
>
0.0
:
p
=
10
**
d
return
float
(
math
.
floor
((
x
*
p
)
+
math
.
copysign
(
0.5
,
x
)))
/
p
else
:
p
=
10
**
d
return
float
(
math
.
ceil
((
x
*
p
)
+
math
.
copysign
(
0.5
,
x
)))
/
p
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 divided
Returns:
division result of x // y
"""
return
x
//
y
# exception related functions
def
get_exception_message
(
exc
):
"""
Get the error message of a specific exception
Args:
exec(Exception) : The exception to get error message.
Returns:
the error message of exec
"""
assert
exc
is
not
None
if
six
.
PY2
:
return
exc
.
message
else
:
...
...
python/paddle/fluid/layers/detection.py
浏览文件 @
09103084
...
...
@@ -20,6 +20,7 @@ from .layer_function_generator import autodoc, templatedoc
from
..layer_helper
import
LayerHelper
from
.
import
tensor
from
.
import
nn
from
..
import
compat
as
cpt
import
math
import
six
from
functools
import
reduce
...
...
@@ -1104,7 +1105,8 @@ def multi_box_head(inputs,
mbox_loc
=
nn
.
transpose
(
mbox_loc
,
perm
=
[
0
,
2
,
3
,
1
])
new_shape
=
[
mbox_loc
.
shape
[
0
],
mbox_loc
.
shape
[
1
]
*
mbox_loc
.
shape
[
2
]
*
mbox_loc
.
shape
[
3
]
/
4
,
4
mbox_loc
.
shape
[
1
]
*
mbox_loc
.
shape
[
2
]
*
cpt
.
floor_division
(
mbox_loc
.
shape
[
3
],
4
),
4
]
mbox_loc_flatten
=
nn
.
reshape
(
mbox_loc
,
shape
=
new_shape
)
mbox_locs
.
append
(
mbox_loc_flatten
)
...
...
@@ -1119,8 +1121,9 @@ def multi_box_head(inputs,
stride
=
stride
)
conf_loc
=
nn
.
transpose
(
conf_loc
,
perm
=
[
0
,
2
,
3
,
1
])
new_shape
=
[
conf_loc
.
shape
[
0
],
conf_loc
.
shape
[
1
]
*
conf_loc
.
shape
[
2
]
*
conf_loc
.
shape
[
3
]
/
num_classes
,
num_classes
conf_loc
.
shape
[
0
],
conf_loc
.
shape
[
1
]
*
conf_loc
.
shape
[
2
]
*
cpt
.
floor_division
(
conf_loc
.
shape
[
3
],
num_classes
),
num_classes
]
conf_loc_flatten
=
nn
.
reshape
(
conf_loc
,
shape
=
new_shape
)
mbox_confs
.
append
(
conf_loc_flatten
)
...
...
python/paddle/fluid/tests/unittests/test_compat.py
0 → 100644
浏览文件 @
09103084
# 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
unittest
import
paddle.fluid.compat
as
cpt
import
six
class
TestCompatible
(
unittest
.
TestCase
):
def
test_to_literal_str
(
self
):
# Only support python2.x and python3.x now
self
.
assertTrue
(
six
.
PY2
|
six
.
PY3
)
if
six
.
PY2
:
# check None
self
.
assertIsNone
(
cpt
.
to_literal_str
(
None
))
# check all string related types
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
str
(
""
)),
unicode
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
str
(
"123"
)),
unicode
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
b
""
),
unicode
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
b
""
),
unicode
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
u
""
),
unicode
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
u
""
),
unicode
))
self
.
assertEqual
(
u
""
,
cpt
.
to_literal_str
(
str
(
""
)))
self
.
assertEqual
(
u
"123"
,
cpt
.
to_literal_str
(
str
(
"123"
)))
self
.
assertEqual
(
u
""
,
cpt
.
to_literal_str
(
b
""
))
self
.
assertEqual
(
u
"123"
,
cpt
.
to_literal_str
(
b
"123"
))
self
.
assertEqual
(
u
""
,
cpt
.
to_literal_str
(
u
""
))
self
.
assertEqual
(
u
"123"
,
cpt
.
to_literal_str
(
u
"123"
))
# check list types, not inplace
l
=
[
""
]
l2
=
cpt
.
to_literal_str
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
u
""
],
l2
)
l
=
[
""
,
"123"
]
l2
=
cpt
.
to_literal_str
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
u
""
,
u
"123"
],
l2
)
l
=
[
""
,
b
'123'
,
u
"321"
]
l2
=
cpt
.
to_literal_str
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
u
""
,
u
"123"
,
u
"321"
],
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
unicode
))
# check list types, inplace
l
=
[
""
]
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
u
""
],
l2
)
l
=
[
""
,
"123"
]
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
u
""
,
u
"123"
],
l2
)
l
=
[
""
,
b
"123"
,
u
"321"
]
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
u
""
,
u
"123"
,
u
"321"
],
l2
)
# check set types, not inplace
l
=
set
(
""
)
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
u
""
),
l2
)
l
=
set
([
b
""
,
b
"123"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
u
""
,
u
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
u
""
,
u
"123"
,
u
"321"
]),
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
unicode
))
# check set types, inplace
l
=
set
(
""
)
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
u
""
),
l2
)
l
=
set
([
b
""
,
b
"123"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
u
""
,
u
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
u
""
,
u
"123"
,
u
"321"
]),
l2
)
elif
six
.
PY3
:
self
.
assertIsNone
(
cpt
.
to_literal_str
(
None
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
str
(
""
)),
str
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
str
(
"123"
)),
str
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
b
""
),
str
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
b
""
),
str
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
u
""
),
str
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_literal_str
(
u
""
),
str
))
self
.
assertEqual
(
""
,
cpt
.
to_literal_str
(
str
(
""
)))
self
.
assertEqual
(
"123"
,
cpt
.
to_literal_str
(
str
(
"123"
)))
self
.
assertEqual
(
""
,
cpt
.
to_literal_str
(
b
""
))
self
.
assertEqual
(
"123"
,
cpt
.
to_literal_str
(
b
"123"
))
self
.
assertEqual
(
""
,
cpt
.
to_literal_str
(
u
""
))
self
.
assertEqual
(
"123"
,
cpt
.
to_literal_str
(
u
"123"
))
# check list types, not inplace
l
=
[
""
]
l2
=
cpt
.
to_literal_str
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
""
],
l2
)
l
=
[
""
,
"123"
]
l2
=
cpt
.
to_literal_str
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
""
,
"123"
],
l2
)
l
=
[
""
,
b
"123"
,
u
"321"
]
l2
=
cpt
.
to_literal_str
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
([
""
,
"123"
,
"321"
],
l2
)
# check list types, inplace
l
=
[
""
]
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
""
],
l2
)
l
=
[
""
,
b
"123"
]
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
""
,
"123"
],
l2
)
l
=
[
""
,
b
"123"
,
u
"321"
]
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
""
,
"123"
,
"321"
],
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
str
))
# check set types, not inplace
l
=
set
(
""
)
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
""
),
l2
)
l
=
set
([
b
""
,
b
"123"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
""
,
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
""
,
"123"
,
"321"
]),
l2
)
# check set types, inplace
l
=
set
(
""
)
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
""
),
l2
)
l
=
set
([
b
""
,
b
"123"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
""
,
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_literal_str
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
""
,
"123"
,
"321"
]),
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
str
))
def
test_to_bytes
(
self
):
# Only support python2.x and python3.x now
self
.
assertTrue
(
six
.
PY2
|
six
.
PY3
)
if
six
.
PY2
:
# check None
self
.
assertIsNone
(
cpt
.
to_bytes
(
None
))
# check all string related types
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
str
(
""
)),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
str
(
"123"
)),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
b
""
),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
b
""
),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
u
""
),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
u
""
),
bytes
))
self
.
assertEqual
(
b
""
,
cpt
.
to_bytes
(
str
(
""
)))
self
.
assertEqual
(
b
"123"
,
cpt
.
to_bytes
(
str
(
"123"
)))
self
.
assertEqual
(
b
""
,
cpt
.
to_bytes
(
b
""
))
self
.
assertEqual
(
b
"123"
,
cpt
.
to_bytes
(
b
"123"
))
self
.
assertEqual
(
b
""
,
cpt
.
to_bytes
(
u
""
))
self
.
assertEqual
(
b
"123"
,
cpt
.
to_bytes
(
u
"123"
))
# check list types, not inplace
l
=
[
""
]
l2
=
cpt
.
to_bytes
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
],
l2
)
l
=
[
""
,
"123"
]
l2
=
cpt
.
to_bytes
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
],
l2
)
l
=
[
""
,
b
'123'
,
u
"321"
]
l2
=
cpt
.
to_bytes
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
,
b
"321"
],
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
bytes
))
# check list types, inplace
l
=
[
""
]
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
],
l2
)
l
=
[
""
,
"123"
]
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
],
l2
)
l
=
[
""
,
b
"123"
,
u
"321"
]
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
,
b
"321"
],
l2
)
# check set types, not inplace
l
=
set
(
""
)
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
b
""
),
l2
)
l
=
set
([
b
""
,
b
"123"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
,
b
"321"
]),
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
bytes
))
# check set types, inplace
l
=
set
(
""
)
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
b
""
),
l2
)
l
=
set
([
b
""
,
b
"123"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
,
b
"321"
]),
l2
)
elif
six
.
PY3
:
self
.
assertIsNone
(
cpt
.
to_bytes
(
None
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
str
(
""
)),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
str
(
"123"
)),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
b
""
),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
b
""
),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
u
""
),
bytes
))
self
.
assertTrue
(
isinstance
(
cpt
.
to_bytes
(
u
""
),
bytes
))
self
.
assertEqual
(
b
""
,
cpt
.
to_bytes
(
str
(
""
)))
self
.
assertEqual
(
b
"123"
,
cpt
.
to_bytes
(
str
(
"123"
)))
self
.
assertEqual
(
b
""
,
cpt
.
to_bytes
(
b
""
))
self
.
assertEqual
(
b
"123"
,
cpt
.
to_bytes
(
b
"123"
))
self
.
assertEqual
(
b
""
,
cpt
.
to_bytes
(
u
""
))
self
.
assertEqual
(
b
"123"
,
cpt
.
to_bytes
(
u
"123"
))
# check list types, not inplace
l
=
[
""
]
l2
=
cpt
.
to_bytes
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
],
l2
)
l
=
[
""
,
"123"
]
l2
=
cpt
.
to_bytes
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
],
l2
)
l
=
[
""
,
b
"123"
,
u
"321"
]
l2
=
cpt
.
to_bytes
(
l
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
,
b
"321"
],
l2
)
# check list types, inplace
l
=
[
""
]
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
],
l2
)
l
=
[
""
,
b
"123"
]
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
],
l2
)
l
=
[
""
,
b
"123"
,
u
"321"
]
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
list
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
([
b
""
,
b
"123"
,
b
"321"
],
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
bytes
))
# check set types, not inplace
l
=
set
([
""
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
]),
l2
)
l
=
set
([
u
""
,
u
"123"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
False
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertFalse
(
l
is
l2
)
self
.
assertNotEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
,
b
"321"
]),
l2
)
# check set types, inplace
l
=
set
(
""
)
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
(
b
""
),
l2
)
l
=
set
([
u
""
,
u
"123"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
]),
l2
)
l
=
set
([
""
,
b
"123"
,
u
"321"
])
l2
=
cpt
.
to_bytes
(
l
,
inplace
=
True
)
self
.
assertTrue
(
isinstance
(
l2
,
set
))
self
.
assertTrue
(
l
is
l2
)
self
.
assertEqual
(
l
,
l2
)
self
.
assertEqual
(
set
([
b
""
,
b
"123"
,
b
"321"
]),
l2
)
for
i
in
l2
:
self
.
assertTrue
(
isinstance
(
i
,
bytes
))
def
test_round
(
self
):
self
.
assertEqual
(
3.0
,
cpt
.
round
(
3.4
))
self
.
assertEqual
(
4.0
,
cpt
.
round
(
3.5
))
self
.
assertEqual
(
0.0
,
cpt
.
round
(
0.1
))
self
.
assertEqual
(
-
0.0
,
cpt
.
round
(
-
0.1
))
self
.
assertEqual
(
-
3.0
,
cpt
.
round
(
-
3.4
))
self
.
assertEqual
(
-
4.0
,
cpt
.
round
(
-
3.5
))
self
.
assertEqual
(
5.0
,
cpt
.
round
(
5
))
self
.
assertRaises
(
TypeError
,
cpt
.
round
,
None
)
def
test_floor_division
(
self
):
self
.
assertEqual
(
0.0
,
cpt
.
floor_division
(
3
,
4
))
self
.
assertEqual
(
1.0
,
cpt
.
floor_division
(
4
,
3
))
self
.
assertEqual
(
2.0
,
cpt
.
floor_division
(
6
,
3
))
self
.
assertEqual
(
-
2.0
,
cpt
.
floor_division
(
-
4
,
3
))
self
.
assertEqual
(
-
2.0
,
cpt
.
floor_division
(
-
6
,
3
))
self
.
assertRaises
(
ZeroDivisionError
,
cpt
.
floor_division
,
3
,
0
)
self
.
assertRaises
(
TypeError
,
cpt
.
floor_division
,
None
,
None
)
def
test_get_exception_message
(
self
):
exception_message
=
"test_message"
self
.
assertRaises
(
AssertionError
,
cpt
.
get_exception_message
,
None
)
if
six
.
PY2
:
self
.
assertRaises
(
AttributeError
,
cpt
.
get_exception_message
,
exception_message
)
try
:
raise
RuntimeError
(
exception_message
)
except
Exception
as
e
:
self
.
assertEqual
(
exception_message
,
cpt
.
get_exception_message
(
e
))
self
.
assertIsNotNone
(
e
)
try
:
raise
Exception
(
exception_message
)
except
Exception
as
e
:
self
.
assertEqual
(
exception_message
,
cpt
.
get_exception_message
(
e
))
self
.
assertIsNotNone
(
e
)
if
six
.
PY3
:
try
:
raise
RuntimeError
(
exception_message
)
except
Exception
as
e
:
self
.
assertEqual
(
exception_message
,
cpt
.
get_exception_message
(
e
))
self
.
assertIsNotNone
(
e
)
try
:
raise
Exception
(
exception_message
)
except
Exception
as
e
:
self
.
assertEqual
(
exception_message
,
cpt
.
get_exception_message
(
e
))
self
.
assertIsNotNone
(
e
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录