def test_assign_from_struct(): a = Duck() a[:] = [2, 3, 4, 5] assert list(a.items()) == [(0, 2), (1, 3), (2, 4), (3, 5)] a[:] = dict(a=1, b=2, c=3) assert list(a.items()) == [('a', 1), ('b', 2), ('c', 3)]
def test_assign_from_struct(): a = Duck() a[:] = [2, 3, 4, 5] assert list(a.items()) == [(0, 2), (1, 3), (2, 4), (3, 5)] a[:] = dict(a=1, b=2, c=3) assert list(a.items()) == [('a', 1), ('b', 2), ('c', 3)]
def test_slice_assignment(): deep_struct = {'a': [1, {'c': 2, 'd': 3}], 'b': [1, 2]} a = Duck() a[:] = deep_struct assert list(a.items(depth='full')) == [(('a', ), [1, { 'c': 2, 'd': 3 }]), (('b', ), [1, 2])] a = Duck() a[:, :] = deep_struct assert list(a.items(depth='full')) == [(('a', 0), 1), (('a', 1), { 'c': 2, 'd': 3 }), (('b', 0), 1), (('b', 1), 2)] a = Duck() with raises(InitializerTooShallowError): a[:, :, :] = deep_struct a = Duck() a[...] = deep_struct assert list(a.items(depth='full')) == [(('a', 0), 1), (('a', 1, 'c'), 2), (('a', 1, 'd'), 3), (('b', 0), 1), (('b', 1), 2)] a = Duck() with raises( InvalidKeyError ): # It would be nice to add this functionality, but maybe later. a[..., next] = deep_struct
def test_slice_assignment(): deep_struct = {'a': [1,{'c': 2, 'd': 3}], 'b': [1, 2]} a = Duck() a[:] = deep_struct assert list(a.items(depth='full')) == [ (('a', ), [1,{'c': 2, 'd': 3}]), (('b', ), [1, 2]) ] a = Duck() a[:, :] = deep_struct assert list(a.items(depth='full')) == [ (('a', 0), 1), (('a', 1), {'c': 2, 'd': 3}), (('b', 0), 1), (('b', 1), 2) ] a = Duck() with raises(InitializerTooShallowError): a[:, :, :] = deep_struct a = Duck() a[...] = deep_struct assert list(a.items(depth='full')) == [ (('a', 0), 1), (('a', 1, 'c'), 2), (('a', 1, 'd'), 3), (('b', 0), 1), (('b', 1), 2) ] a = Duck() with raises(InvalidKeyError): # It would be nice to add this functionality, but maybe later. a[..., next] = deep_struct