本文整理汇总了Python中flatland.Dict类的典型用法代码示例。如果您正苦于以下问题:Python Dict类的具体用法?Python Dict怎么用?Python Dict使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Dict类的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_nested_dict_as_unicode
def test_nested_dict_as_unicode():
schema = Dict.of(Dict.named(u'd').of(
Integer.named(u'x').using(default=10)))
el = schema.from_defaults()
eq_(el.value, {u'd': {u'x': 10}})
eq_(el.u, u"{u'd': {u'x': u'10'}}")
示例3: test_path
def test_path():
schema = Dict.named('root').of(
String.named('element'),
Dict.named('dict').of(String.named('dict_element')))
element = schema()
assert (list(element.el(['dict', 'dict_element']).path) ==
[element, element['dict'], element['dict']['dict_element']])
示例4: elements
def elements():
from flatland import Dict, String
anon = Dict.of(String.named(u'anon_field'))()
named = Dict.named(u'named').of(String.named(u'named_field'))()
prefixed = Dict.named(u'prefixed').of(String.named(u'prefixed_field'))()
prefixed.set_prefix(u'three.levels.down')
return {'form': anon,
'forms': {'named': named },
'three': {'levels': {'down': prefixed}}}
示例5: test_simple_validation_shortcircuit
def test_simple_validation_shortcircuit():
Regular = Dict.of(Integer.using(optional=False))
el = Regular()
assert not el.validate()
def boom(element, state):
assert False
all_ok = lambda element, state: SkipAll
Boom = Integer.named(u'i').using(validators=[boom])
ShortCircuited = Dict.of(Boom).using(descent_validators=[all_ok])
el = ShortCircuited()
assert el.validate()
示例6: test_naming_dict
def test_naming_dict():
for name, root_flat, leaf_flat in ((u'd', u'd', u'd_s'),
(None, u'', u's')):
schema = Dict.named(name).of(String.named(u's'))
root = schema()
leaf = root[u's']
assert root.fq_name() == u'.'
assert root.flattened_name() == root_flat
assert root.el(u'.') is root
assert leaf.fq_name() == u'.s'
assert leaf.flattened_name() == leaf_flat
assert root.el(u'.s') is leaf
assert root.el(u's') is leaf
assert leaf.el(u'.s') is leaf
with pytest.raises(LookupError):
leaf.el(u's')
assert leaf.el(u'.') is root
assert root.el([Root]) is root
assert root.el([u's']) is leaf
assert root.el([Root, u's']) is leaf
assert root.el(iter([u's'])) is leaf
assert root.el(iter([Root, u's'])) is leaf
示例7: 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})
示例8: test_dict_el
def test_dict_el():
# stub
schema = Dict.named(u's').of(Integer.named(u'x'), Integer.named(u'y'))
element = schema()
assert element.el(u'x').name == u'x'
assert_raises(KeyError, element.el, u'not_x')
示例9: test_update_object
def test_update_object():
class Obj(object):
def __init__(self, **kw):
for (k, v) in kw.items():
setattr(self, k, v)
schema = Dict.of(String.named(u'x'), String.named(u'y'))
o = Obj()
assert not hasattr(o, 'x')
assert not hasattr(o, 'y')
def updated_(obj_factory, initial_value, wanted=None, **update_kw):
el = schema(initial_value)
obj = obj_factory()
update_kw.setdefault('key', asciistr)
el.update_object(obj, **update_kw)
if wanted is None:
wanted = dict((asciistr(k), v) for k, v in initial_value.items())
have = dict(obj.__dict__)
assert have == wanted
updated_(Obj, {u'x': u'X', u'y': u'Y'})
updated_(Obj, {u'x': u'X'}, {'x': u'X', 'y': None})
updated_(lambda: Obj(y=u'Y'), {u'x': u'X'}, {'x': u'X', 'y': None})
updated_(lambda: Obj(y=u'Y'), {u'x': u'X'}, {'x': u'X', 'y': u'Y'},
omit=('y',))
updated_(lambda: Obj(y=u'Y'), {u'x': u'X'}, {'y': u'Y'},
include=(u'z',))
updated_(Obj, {u'x': u'X'}, {'y': None, 'z': u'X'},
rename=(('x', 'z'),))
示例10: user_filter_form
def user_filter_form(values=None):
schema = Dict.of(
String.named(u'field1').using(optional=True),
String.named(u'field2'))
if values is None:
values = {
u'field1': u'val',
}
el = schema(values)
el[u'field2'].errors.append(u'Missing')
def label_filter(tag, attrs, stream, context, el):
from genshi import QName
if el is None:
return tag, attrs, stream
content = stream.render()
if not content:
label = el.label
if not el.optional:
label += ' *'
stream = label
if not el.optional:
css = (attrs.get(u'class', u'') + u' required').strip()
attrs |= [(QName(u'class'), css)]
return tag, attrs, stream
label_filter.tags = (u'label',)
return dict(form=el, label_filter=label_filter)
示例11: test_descent
def test_descent(self):
schema = Dict.of(Integer).using(
descent_validators=[self.validator('1', True)])
el = schema()
assert not el.validate()
eq_(self.canary, ['1'])
assert el.valid
assert not el.all_valid
示例12: test_tuple_plural
def test_tuple_plural():
catalog = GetTextish()
schema = Dict.of(String.named('name').
using(validators=[LocalizedShort(2)]))
data = schema(dict(name='xxx'))
data.validate(catalog)
assert data['name'].errors == [u'plural NAME 2']
示例13: test_not_writable
def test_not_writable():
schema = Dict.of(Integer.named(u'main'),
Ref.named(u'aux').to(u'main').using(writable=False))
el = schema()
with pytest.raises(TypeError):
el[u'aux'].set(6)
示例14: test_writable
def test_writable():
schema = Dict.of(Integer.named(u'main'),
Ref.named(u'aux').to(u'main').using(writable=True))
el = schema()
el[u'aux'] = 6
assert el[u'main'].value == 6
assert el[u'aux'].value == 6
示例15: test_compound_optional
def test_compound_optional():
required = Dict.of(DateYYYYMMDD.named(u's').using(optional=False))
f = required.from_defaults()
assert not f.el(u's.year').optional
assert not f.el(u's.month').optional
assert not f.el(u's.day').optional
assert not f.validate()
optional = Dict.of(DateYYYYMMDD.named(u's').using(optional=True))
f = optional.from_defaults()
assert f.el(u's.year').optional
assert f.el(u's.month').optional
assert f.el(u's.day').optional
assert f.validate()