本文整理汇总了Python中flatland.String.named方法的典型用法代码示例。如果您正苦于以下问题:Python String.named方法的具体用法?Python String.named怎么用?Python String.named使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flatland.String
的用法示例。
在下文中一共展示了String.named方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_update_object
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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'),))
示例2: user_filter_form
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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)
示例3: test_path
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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: shadow
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
def shadow():
schema = Dict.named(u'dict_name').of(
String.named(u'name'),
String.named(u'u'))
element = schema({u'name': u'string name',
u'u': u'string u'})
element.set_prefix(u'top')
return {'top': element, 'bound': element.bind}
示例5: elements
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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}}}
示例6: test_set_flat_doubly_anonymous_dict
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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})
示例7: form
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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}
示例8: test_naming_list_list
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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
示例9: _populate_app_fields
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
def _populate_app_fields(self):
with WindowServiceProxy(59000) as w:
self.video_mode_map = w.get_video_mode_map()
if self.video_mode_map:
self._video_available = True
else:
self._video_available = False
self.video_mode_keys = sorted(self.video_mode_map.keys())
if self._video_available:
self.device_key, self.devices = w.get_video_source_configs()
field_list = [
Integer.named('overlay_opacity').using(default=50, optional=True),
Directory.named('device_directory').using(default='', optional=True),
String.named('transform_matrix').using(default='', optional=True,
properties={'show_in_gui':
False}), ]
if self._video_available:
video_mode_enum = Enum.named('video_mode').valued(
*self.video_mode_keys).using(default=self.video_mode_keys[0],
optional=True)
video_enabled_boolean = Boolean.named('video_enabled').using(
default=False, optional=True, properties={'show_in_gui': True})
recording_enabled_boolean = Boolean.named('recording_enabled').using(
default=False, optional=True, properties={'show_in_gui': False})
field_list.append(video_mode_enum)
field_list.append(video_enabled_boolean)
field_list.append(recording_enabled_boolean)
return Form.of(*field_list)
示例10: test_compose_abstract_fixme
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
def test_compose_abstract_fixme():
with pytest.raises(TypeError):
# really it'd be nice if serialize simply wasn't inherited. would
# have to rejigger the hierarchy, not sure its worth it.
schema = Compound.using(field_schema=[String.named(u'y')])
el = schema()
schema.serialize(el, u'abc')
示例11: test_naming_dict
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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
示例12: test_tuple_plural
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
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_el
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
def test_el():
schema = Array.of(String.named(u's'))
element = schema(u'abc')
eq_(list(element.value), [u'a', u'b', u'c'])
eq_(element.el(u'0').value, u'a')
eq_(element.el(u'2').value, u'c')
assert_raises(KeyError, element.el, u'a')
示例14: test_el
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
def test_el():
schema = Array.of(String.named(u's'))
element = schema(u'abc')
assert list(element.value) == [u'a', u'b', u'c']
assert element.el(u'0').value == u'a'
assert element.el(u'2').value == u'c'
with pytest.raises(KeyError):
element.el(u'a')
示例15: test_slice
# 需要导入模块: from flatland import String [as 别名]
# 或者: from flatland.String import named [as 别名]
def test_slice():
schema = Dict.of(String.named(u'x'), String.named(u'y'))
def same_(source, kw):
el = schema(source)
sliced = el.slice(**kw)
wanted = dict(keyslice_pairs(el.value.items(), **kw))
eq_(sliced, wanted)
eq_(set(type(_) for _ in sliced.keys()),
set(type(_) for _ in wanted.keys()))
yield same_, {u'x': u'X', u'y': u'Y'}, {}
yield same_, {u'x': u'X', u'y': u'Y'}, dict(key=asciistr)
yield same_, {u'x': u'X', u'y': u'Y'}, dict(include=[u'x'])
yield same_, {u'x': u'X', u'y': u'Y'}, dict(omit=[u'x'])
yield same_, {u'x': u'X', u'y': u'Y'}, dict(omit=[u'x'],
rename={u'y': u'z'})