def test_dictproxy():
# This is the dictproxy constructor itself from the Python API,
DP = ctypes.pythonapi.PyDictProxy_New
DP.argtypes, DP.restype = (ctypes.py_object,), ctypes.py_object
underlying_dict = {}
mp_recursive = DP(underlying_dict)
underlying_dict[0] = mp_recursive
underlying_dict[-3] = underlying_dict
cases = [
(DP({}), "dict_proxy({})"),
(DP({None: DP({})}), "dict_proxy({None: dict_proxy({})})"),
(DP({k: k.lower() for k in string.ascii_uppercase}),
"dict_proxy({'A': 'a',\n"
" 'B': 'b',\n"
" 'C': 'c',\n"
" 'D': 'd',\n"
" 'E': 'e',\n"
" 'F': 'f',\n"
" 'G': 'g',\n"
" 'H': 'h',\n"
" 'I': 'i',\n"
" 'J': 'j',\n"
" 'K': 'k',\n"
" 'L': 'l',\n"
" 'M': 'm',\n"
" 'N': 'n',\n"
" 'O': 'o',\n"
" 'P': 'p',\n"
" 'Q': 'q',\n"
" 'R': 'r',\n"
" 'S': 's',\n"
" 'T': 't',\n"
" 'U': 'u',\n"
" 'V': 'v',\n"
" 'W': 'w',\n"
" 'X': 'x',\n"
" 'Y': 'y',\n"
" 'Z': 'z'})"),
(mp_recursive, "dict_proxy({-3: {-3: {...}, 0: {...}}, 0: {...}})"),
]
for obj, expected in cases:
nt.assert_is_instance(obj, types.DictProxyType) # Meta-test
nt.assert_equal(pretty.pretty(obj), expected)
nt.assert_equal(pretty.pretty(underlying_dict),
"{-3: {...}, 0: dict_proxy({-3: {...}, 0: {...}})}")
python类assert_is_instance()的实例源码
def test_dictproxy():
# This is the dictproxy constructor itself from the Python API,
DP = ctypes.pythonapi.PyDictProxy_New
DP.argtypes, DP.restype = (ctypes.py_object,), ctypes.py_object
underlying_dict = {}
mp_recursive = DP(underlying_dict)
underlying_dict[0] = mp_recursive
underlying_dict[-3] = underlying_dict
cases = [
(DP({}), "dict_proxy({})"),
(DP({None: DP({})}), "dict_proxy({None: dict_proxy({})})"),
(DP({k: k.lower() for k in string.ascii_uppercase}),
"dict_proxy({'A': 'a',\n"
" 'B': 'b',\n"
" 'C': 'c',\n"
" 'D': 'd',\n"
" 'E': 'e',\n"
" 'F': 'f',\n"
" 'G': 'g',\n"
" 'H': 'h',\n"
" 'I': 'i',\n"
" 'J': 'j',\n"
" 'K': 'k',\n"
" 'L': 'l',\n"
" 'M': 'm',\n"
" 'N': 'n',\n"
" 'O': 'o',\n"
" 'P': 'p',\n"
" 'Q': 'q',\n"
" 'R': 'r',\n"
" 'S': 's',\n"
" 'T': 't',\n"
" 'U': 'u',\n"
" 'V': 'v',\n"
" 'W': 'w',\n"
" 'X': 'x',\n"
" 'Y': 'y',\n"
" 'Z': 'z'})"),
(mp_recursive, "dict_proxy({-3: {-3: {...}, 0: {...}}, 0: {...}})"),
]
for obj, expected in cases:
nt.assert_is_instance(obj, types.DictProxyType) # Meta-test
nt.assert_equal(pretty.pretty(obj), expected)
nt.assert_equal(pretty.pretty(underlying_dict),
"{-3: {...}, 0: dict_proxy({-3: {...}, 0: {...}})}")
def test_display_id():
ip = get_ipython()
with mock.patch.object(ip.display_pub, 'publish') as pub:
handle = display.display('x')
nt.assert_is(handle, None)
handle = display.display('y', display_id='secret')
nt.assert_is_instance(handle, display.DisplayHandle)
handle2 = display.display('z', display_id=True)
nt.assert_is_instance(handle2, display.DisplayHandle)
nt.assert_not_equal(handle.display_id, handle2.display_id)
nt.assert_equal(pub.call_count, 3)
args, kwargs = pub.call_args_list[0]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('x')
},
'metadata': {},
})
args, kwargs = pub.call_args_list[1]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('y')
},
'metadata': {},
'transient': {
'display_id': handle.display_id,
},
})
args, kwargs = pub.call_args_list[2]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('z')
},
'metadata': {},
'transient': {
'display_id': handle2.display_id,
},
})