本文整理汇总了Python中flatland.List类的典型用法代码示例。如果您正苦于以下问题:Python List类的具体用法?Python List怎么用?Python List使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了List类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mixed_all_children
def test_mixed_all_children():
data = {u'A1': u'1',
u'A2': u'2',
u'A3': {u'A3B1': {u'A3B1C1': u'1',
u'A3B1C2': u'2'},
u'A3B2': {u'A3B2C1': u'1'},
u'A3B3': [[u'A3B3C0D0', u'A3B3C0D1'],
[u'A3B3C1D0', u'A3B3C1D1'],
[u'A3B3C2D0', u'A3B3C2D1']]}}
schema = (
Dict.named(u'R').of(
String.named(u'A1'),
String.named(u'A2'),
Dict.named(u'A3').of(
Dict.named(u'A3B1').of(
String.named(u'A3B1C1'),
String.named(u'A3B1C2')),
Dict.named(u'A3B2').of(
String.named(u'A3B2C1')),
List.named(u'A3B3').of(
List.named(u'A3B3Cx').of(
String.named(u'A3B3x'))))))
top = schema(data)
names = list(e.name for e in top.all_children)
assert set(names[0:3]) == set([u'A1', u'A2', u'A3'])
assert set(names[3:6]) == set([u'A3B1', u'A3B2', u'A3B3'])
assert set(names[6:12]) == set([u'A3B1C1', u'A3B1C2',
u'A3B2C1', u'A3B3Cx'])
assert set(names[12:]) == set([u'A3B3x'])
assert len(names[12:]) == 6
示例2: test_naming_list_list
def test_naming_list_list():
# make sure nested Slots-users don't bork
for name, root_flat, leaf_flat in ((u'l', u'l', u'l_0_l2_0_s'),
(None, u'', u'0_l2_0_s')):
schema = List.named(name).of(List.named(u'l2').of(String.named(u's')))
root = schema([u'x'])
leaf = root[0][0]
assert root.fq_name() == u'.'
assert root.flattened_name() == root_flat
assert root.el(u'.') is root
assert leaf.fq_name() == u'.0.0'
assert leaf.flattened_name() == leaf_flat
assert root.el(u'.0.0') is leaf
assert root.el(u'0.0') is leaf
assert leaf.el(u'.0.0') is leaf
with pytest.raises(LookupError):
leaf.el(u'0')
with pytest.raises(LookupError):
leaf.el(u's')
assert leaf.el(u'.') is root
assert root.el([u'0', u'0']) is leaf
示例3: form
def form():
schema = Dict.named(u'field0').of(
String.named(u'field1'),
String.named(u'field2'),
List.named(u'field3').of(String.named(u'field4')),
List.named(u'field5').of(
List.named(u'field6').of(String.named(u'field7'))))
element = schema(
{u'field1': u'val1',
u'field2': u'val2',
u'field3': [u'val3'],
u'field5': [[u'val4']]})
element.set_prefix(u'form')
return {'form': element, 'bound': element.bind}
示例4: test_set
def test_set():
schema = List.of(Integer)
el = schema()
assert list(el) == []
el = schema()
assert not el.set(1)
assert el.value == []
el = schema()
assert not el.set(None)
assert el.value == []
el = schema()
assert el.set(range(3))
assert el.value == [0, 1, 2]
el = schema()
assert el.set(xrange(3))
assert el.value == [0, 1, 2]
el = schema([0, 1, 2])
assert el.value == [0, 1, 2]
el = schema()
el.extend([1, 2, 3])
assert el.value == [1, 2, 3]
el.set([4, 5, 6])
assert el.value == [4, 5, 6]
assert el.set([])
assert el.value == []
示例5: test_access
def test_access():
pairs = ((u'l_0_i', u'10'), (u'l_1_i', u'11'), (u'l_2_i', u'12'),)
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema.from_flat(pairs)
elements = list(Integer.named(u'i')(val)
for val in (u'10', u'11', u'12'))
assert len(el) == 3
assert el[0] == elements[0]
assert el[1] == elements[1]
assert el[2] == elements[2]
assert el[0].value == 10
assert el[:0] == elements[:0]
assert el[:1] == elements[:1]
assert el[0:5] == elements[0:5]
assert el[-2:-1] == elements[-2:-1]
assert el[0] in el
assert elements[0] in el
assert u'10' in el
assert 10 in el
assert el.count(elements[0]) == 1
assert el.count(u'10') == 1
assert el.count(10) == 1
assert el.index(elements[0]) == 0
assert el.index(u'10') == 0
assert el.index(10) == 0
示例6: test_no_duplicates_list_anon_dict
def test_no_duplicates_list_anon_dict():
nd = NotDuplicated(failure=u'%(container_label)s %(position)s')
schema = (List.named('test').
of(Dict.of(Integer.named('x'),
Integer.named('y')).
using(validators=[nd])))
_test_no_duplicates(schema, {'x': 1, 'y': 2}, {'x': 3, 'y': 4})
示例7: test_set_flat_linear
def test_set_flat_linear():
pairs = [(u'l_0_i', 1), (u'l_1_i', 2), (u'l_2_i', 3)]
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema.from_flat(pairs)
eq_(len(el), len(pairs))
eq_(el.value, list(pair[1] for pair in pairs))
示例8: test_reverse
def test_reverse():
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema([2, 1])
assert el.flatten() == [(u'l_0_i', u'2'), (u'l_1_i', u'1')]
el.reverse()
assert el.value == [1, 2]
assert el.flatten() == [(u'l_0_i', u'1'), (u'l_1_i', u'2')]
示例9: test_set_flat_miss
def test_set_flat_miss():
pairs = [(u'l_galump', u'3'), (u'l_snorgle', u'4')]
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema.from_flat(pairs)
eq_(len(el), 0)
eq_(el.value, [])
示例10: test_values_equal_resolution
def test_values_equal_resolution():
v = ValuesEqual('x', '/sub/xx')
el = form(dict(x='a', sub=dict(xx='a')))
assert v.validate(el, None)
v = ValuesEqual('/x', 'xx')
el = form(dict(x='a', sub=dict(xx='a')))
assert v.validate(el['sub'], None)
# unhashable
v = ValuesEqual('a', 'b')
schema = Dict.of(List.named('a').of(String.named('x')),
List.named('b').of(String.named('x')))
el = schema(dict(a=['a', 'b'], b=['a', 'b']))
assert v.validate(el, None)
el = schema(dict(a=['a', 'b'], b=['x', 'y']))
assert not v.validate(el, None)
示例11: test_slots
def test_slots():
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema([1, 2])
assert len(list(el._slots)) == 2
for slot in el._slots:
# don't really care what it says, just no crashy.
assert repr(slot)
assert [slot.value for slot in el._slots] == [1, 2]
示例12: test_sort
def test_sort():
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema([2, 1])
el.sort(key=lambda el: el.value)
assert el.value == [1, 2]
assert el.flatten() == [(u'l_0_i', u'1'), (u'l_1_i', u'2')]
el.sort(key=lambda el: el.value, reverse=True)
assert el.value == [2, 1]
assert el.flatten() == [(u'l_0_i', u'2'), (u'l_1_i', u'1')]
示例13: test_set_flat_doubly_anonymous_dict
def test_set_flat_doubly_anonymous_dict():
pairs = ((u'0_x', u'x0'), (u'0_y', u'y0'),
(u'1_x', u'x1'), (u'1_y', u'y1'),
(u'2_x', u'x2'), )
schema = List.of(String.named(u'x'), String.named(u'y'))
el = schema.from_flat(pairs)
eq_(len(el), 3)
eq_(el[0].value, dict((k[-1], v) for k, v in pairs[0:2]))
eq_(el[1].value, dict((k[-1], v) for k, v in pairs[2:4]))
eq_(el[2].value, {u'x': u'x2', u'y': None})
示例14: test_mutation
def test_mutation():
schema = List.named(u'l').of(Integer.named(u'i'))
el = schema()
new_element = Integer.named(u'i')
def order_ok():
slot_names = list(_.name for _ in el._slots)
for idx, name in enumerate(slot_names):
assert name == str(idx).decode('ascii')
assert not el
order_ok()
# FIXME:? seems to want parsable data, not elements
el.append(new_element(u'0'))
assert el.value == [0]
order_ok()
el.append(u'123')
assert el.value == [0, 123]
order_ok()
el.extend([u'4', u'5'])
assert el.value == [0, 123, 4, 5]
order_ok()
el[0] = u'3'
assert el.value == [3, 123, 4, 5]
order_ok()
el.insert(0, u'2')
assert el.value == [2, 3, 123, 4, 5]
order_ok()
v = el.pop()
assert v.value == 5
assert not v.parent
order_ok()
v = el.pop(0)
assert v.value == 2
assert el.value == [3, 123, 4]
order_ok()
el.remove(u'3')
assert el.value == [123, 4]
order_ok()
del el[:]
assert el.value == []
order_ok()
示例15: test_corrupt_all_children
def test_corrupt_all_children():
# Ensure all_children won't spin out if the graph becomes cyclic.
schema = List.of(String)
el = schema()
el.append(String(u'x'))
el.append(String(u'y'))
dupe = String(u'z')
el.append(dupe)
el.append(dupe)
assert list(_.value for _ in el.children) == list(u'xyzz')
assert list(_.value for _ in el.all_children) == list(u'xyz')