我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用copy._deepcopy_dispatch()。
def test_spam_lists(self): # Testing spamlist operations... import copy, xxsubtype as spam def spamlist(l, memo=None): import xxsubtype as spam return spam.spamlist(l) # This is an ugly hack: copy._deepcopy_dispatch[spam.spamlist] = spamlist self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b", "__add__") self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") self.sliceop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]", "__getitem__") self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b", "__iadd__") self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__") self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") self.setsliceop_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), spamlist([1,5,6,4]), "a[b:c]=d", "__setitem__") # Test subclassing class C(spam.spamlist): def foo(self): return 1 a = C() self.assertEqual(a, []) self.assertEqual(a.foo(), 1) a.append(100) self.assertEqual(a, [100]) self.assertEqual(a.getstate(), 0) a.setstate(42) self.assertEqual(a.getstate(), 42)
def test_spam_dicts(self): # Testing spamdict operations... import copy, xxsubtype as spam def spamdict(d, memo=None): import xxsubtype as spam sd = spam.spamdict() for k, v in list(d.items()): sd[k] = v return sd # This is an ugly hack: copy._deepcopy_dispatch[spam.spamdict] = spamdict self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") d = spamdict({1:2,3:4}) l1 = [] for i in list(d.keys()): l1.append(i) l = [] for i in iter(d): l.append(i) self.assertEqual(l, l1) l = [] for i in d.__iter__(): l.append(i) self.assertEqual(l, l1) l = [] for i in type(spamdict({})).__iter__(d): l.append(i) self.assertEqual(l, l1) straightd = {1:2, 3:4} spamd = spamdict(straightd) self.unop_test(spamd, 2, "len(a)", "__len__") self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__") self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}), "a[b]=c", "__setitem__") # Test subclassing class C(spam.spamdict): def foo(self): return 1 a = C() self.assertEqual(list(a.items()), []) self.assertEqual(a.foo(), 1) a['foo'] = 'bar' self.assertEqual(list(a.items()), [('foo', 'bar')]) self.assertEqual(a.getstate(), 0) a.setstate(100) self.assertEqual(a.getstate(), 100)
def test_spam_lists(self): # Testing spamlist operations... import copy, xxsubtype as spam def spamlist(l, memo=None): import xxsubtype as spam return spam.spamlist(l) # This is an ugly hack: copy._deepcopy_dispatch[spam.spamlist] = spamlist self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b", "__add__") self.binop_test(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") self.binop_test(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") self.binop_test(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") self.ternop_test(spamlist([1,2,3]), 0, 2, spamlist([1,2]), "a[b:c]", "__getslice__") self.setop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b", "__iadd__") self.setop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") self.unop_test(spamlist([1,2,3]), 3, "len(a)", "__len__") self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") self.binop_test(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") self.set2op_test(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") self.set3op_test(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") # Test subclassing class C(spam.spamlist): def foo(self): return 1 a = C() self.assertEqual(a, []) self.assertEqual(a.foo(), 1) a.append(100) self.assertEqual(a, [100]) self.assertEqual(a.getstate(), 0) a.setstate(42) self.assertEqual(a.getstate(), 42)
def test_spam_dicts(self): # Testing spamdict operations... import copy, xxsubtype as spam def spamdict(d, memo=None): import xxsubtype as spam sd = spam.spamdict() for k, v in d.items(): sd[k] = v return sd # This is an ugly hack: copy._deepcopy_dispatch[spam.spamdict] = spamdict self.binop_test(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__") self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__") self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__") self.binop_test(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__") d = spamdict({1:2,3:4}) l1 = [] for i in d.keys(): l1.append(i) l = [] for i in iter(d): l.append(i) self.assertEqual(l, l1) l = [] for i in d.__iter__(): l.append(i) self.assertEqual(l, l1) l = [] for i in type(spamdict({})).__iter__(d): l.append(i) self.assertEqual(l, l1) straightd = {1:2, 3:4} spamd = spamdict(straightd) self.unop_test(spamd, 2, "len(a)", "__len__") self.unop_test(spamd, repr(straightd), "repr(a)", "__repr__") self.set2op_test(spamdict({1:2,3:4}), 2, 3, spamdict({1:2,2:3,3:4}), "a[b]=c", "__setitem__") # Test subclassing class C(spam.spamdict): def foo(self): return 1 a = C() self.assertEqual(a.items(), []) self.assertEqual(a.foo(), 1) a['foo'] = 'bar' self.assertEqual(a.items(), [('foo', 'bar')]) self.assertEqual(a.getstate(), 0) a.setstate(100) self.assertEqual(a.getstate(), 100)