FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
msgpack/python/test/test_obj.py at java-0.5.1 · WanderWalker/msgpack · GitHub
WanderWalker
msgpack
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
msgpack
/
python
/
test
/
test_obj.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (36 loc) · 1.14 KB
Breadcrumbs
msgpack
/
python
/
test
/
test_obj.py
Copy path
File metadata and controls
46 lines (36 loc) · 1.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# coding: utf-8
from
nose
import
main
from
nose
.
tools
import
*
from
msgpack
import
packs
,
unpacks
def
_decode_complex
(
obj
):
if
'__complex__'
in
obj
:
return
complex
(
obj
[
'real'
],
obj
[
'imag'
])
return
obj
def
_encode_complex
(
obj
):
if
isinstance
(
obj
,
complex
):
return
{
'__complex__'
:
True
,
'real'
:
1
,
'imag'
:
2
}
return
obj
def
test_encode_hook
():
packed
=
packs
([
3
,
1
+
2j
],
default
=
_encode_complex
)
unpacked
=
unpacks
(
packed
)
eq_
(
unpacked
[
1
], {
'__complex__'
:
True
,
'real'
:
1
,
'imag'
:
2
})
def
test_decode_hook
():
packed
=
packs
([
3
, {
'__complex__'
:
True
,
'real'
:
1
,
'imag'
:
2
}])
unpacked
=
unpacks
(
packed
,
object_hook
=
_decode_complex
)
eq_
(
unpacked
[
1
],
1
+
2j
)
@
raises
(
TypeError
)
def
test_bad_hook
():
packed
=
packs
([
3
,
1
+
2j
],
default
=
lambda
o
:
o
)
unpacked
=
unpacks
(
packed
)
def
_arr_to_str
(
arr
):
return
''
.
join
(
str
(
c
)
for
c
in
arr
)
def
test_array_hook
():
packed
=
packs
([
1
,
2
,
3
])
unpacked
=
unpacks
(
packed
,
list_hook
=
_arr_to_str
)
eq_
(
unpacked
,
'123'
)
if
__name__
==
'__main__'
:
test_decode_hook
()
test_encode_hook
()
test_bad_hook
()
test_array_hook
()
Back
|
FazBrowse Home
|
New Git URL