本文整理汇总了Python中_weakref.proxy方法的典型用法代码示例。如果您正苦于以下问题:Python _weakref.proxy方法的具体用法?Python _weakref.proxy怎么用?Python _weakref.proxy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_weakref
的用法示例。
在下文中一共展示了_weakref.proxy方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def __init__(*args, **kwds):
'''Initialize an ordered dictionary. The signature is the same as
regular dictionaries, but keyword arguments are not recommended because
their insertion order is arbitrary.
'''
if not args:
raise TypeError("descriptor '__init__' of 'OrderedDict' object "
"needs an argument")
self = args[0]
args = args[1:]
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
try:
self.__root
except AttributeError:
self.__hardroot = _Link()
self.__root = root = _proxy(self.__hardroot)
root.prev = root.next = root
self.__map = {}
self.__update(*args, **kwds)
示例2: test_proxy_dir
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def test_proxy_dir(self):
# dir on a deletex proxy should return an empty list,
# not throw.
for cls in [NonCallableClass, CallableClass]:
def run_test():
a = cls()
b = _weakref.proxy(a)
self.assertEqual(dir(a), dir(b))
del(a)
return b
prxy = run_test()
gc.collect()
# gc collection seems to take longer on OSX
if is_osx: time.sleep(2)
#This will fail if original object has not been garbage collected.
self.assertEqual(dir(prxy), [])
示例3: test_special_methods
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def test_special_methods(self):
for cls in [NonCallableClass, CallableClass]:
# calling repr should give us weakproxy's repr,
# calling __repr__ should give us the underlying objects
# repr
a = cls()
b = _weakref.proxy(a)
self.assertTrue(repr(b).startswith('<weakproxy at'))
self.assertEqual(repr(a), b.__repr__())
keep_alive(a)
# calling a special method should work
class strable(object):
def __str__(self): return 'abc'
a = strable()
b = _weakref.proxy(a)
self.assertEqual(str(b), 'abc')
keep_alive(a)
示例4: test_equals
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def test_equals(self):
global called
class C:
for method, op in [('__eq__', '=='), ('__gt__', '>'), ('__lt__', '<'), ('__ge__', '>='), ('__le__', '<='), ('__ne__', '!=')]:
exec("""
def %s(self, *args, **kwargs):
global called
called = '%s'
return True
""" % (method, op))
a = C()
x = _weakref.proxy(a)
for op in ('==', '>', '<', '>=', '<=', '!='):
self.assertEqual(eval('a ' + op + ' 3'), True); self.assertEqual(called, op); called = None
if op == '==' or op == '!=':
self.assertEqual(eval('x ' + op + ' 3'), op == '!='); self.assertEqual(called, None)
self.assertEqual(eval('3 ' + op + ' x'), op == '!='); self.assertEqual(called, None)
else:
res1, res2 = eval('x ' + op + ' 3'), eval('3 ' + op + ' x')
self.assertEqual(called, None)
self.assertTrue((res1 == True and res2 == False) or (res1 == False and res2 == True))
示例5: __init__
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def __init__(*args, **kwds):
'''Initialize an ordered dictionary. The signature is the same as
regular dictionaries, but keyword arguments are not recommended because
their insertion order is arbitrary.
'''
if not args:
raise TypeError("descriptor '__init__' of 'OrderedDict' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
try:
self.__root
except AttributeError:
self.__hardroot = _Link()
self.__root = root = _proxy(self.__hardroot)
root.prev = root.next = root
self.__map = {}
self.__update(*args, **kwds)
示例6: __init__
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def __init__(*args, **kwds):
'''Initialize an ordered dictionary. The signature is the same as
regular dictionaries. Keyword argument order is preserved.
'''
if not args:
raise TypeError("descriptor '__init__' of 'OrderedDict' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
try:
self.__root
except AttributeError:
self.__hardroot = _Link()
self.__root = root = _proxy(self.__hardroot)
root.prev = root.next = root
self.__map = {}
self.__update(*args, **kwds)
示例7: test_cp14632
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def test_cp14632(self):
'''
Make sure '_weakref.proxy(...)==xyz' does not throw after '...'
has been deleted.
'''
def helper_func():
class C:
def __eq__(self, *args, **kwargs): return True
a = C()
self.assertTrue(C()==3)
x = _weakref.proxy(a)
y = _weakref.proxy(a)
self.assertEqual(x, y)
keep_alive(a) #Just to keep 'a' alive up to this point.
return x, y
x, y = helper_func()
gc.collect()
self.assertRaises(ReferenceError, lambda: x==3)
self.assertRaises(ReferenceError, lambda: x==y)
示例8: test_comparison
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def test_comparison(self):
global called
class C:
for method, op in [('__eq__', '=='), ('__gt__', '>'), ('__lt__', '<'), ('__ge__', '>='), ('__le__', '<='), ('__ne__', '!=')]:
exec("""
def %s(self, *args, **kwargs):
global called
called = '%s'
return True
""" % (method, op))
class D(C):
def __call__(self):
pass
A = [ C(), D() ]
X = [ _weakref.proxy(a) for a in A ]
for (a, x) in zip(A, X):
for op in ('==', '>', '<', '>=', '<=', '!='):
self.assertEqual(eval('a ' + op + ' 3'), True); self.assertEqual(called, op); called = None
self.assertEqual(eval('x ' + op + ' 3'), True); self.assertEqual(called, op); called = None
示例9: __setitem__
# 需要导入模块: import _weakref [as 别名]
# 或者: from _weakref import proxy [as 别名]
def __setitem__(self, key, value,
dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
'od.__setitem__(i, y) <==> od[i]=y'
# Setting a new item creates a new link at the end of the linked list,
# and the inherited dictionary is updated with the new key/value pair.
if key not in self:
self.__map[key] = link = Link()
root = self.__root
last = root.prev
link.prev, link.next, link.key = last, root, key
last.next = link
root.prev = proxy(link)
dict_setitem(self, key, value)