本文整理汇总了Python中trafaret.List方法的典型用法代码示例。如果您正苦于以下问题:Python trafaret.List方法的具体用法?Python trafaret.List怎么用?Python trafaret.List使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trafaret
的用法示例。
在下文中一共展示了trafaret.List方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_field
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def build_field(key, value, relations=None):
extra = None
name = key
if isinstance(value, t.ToInt):
v = "number"
elif isinstance(value, (t.String, t.URL)):
v = "string"
elif isinstance(value, t.Email):
v = "email"
elif isinstance(value, t.ToFloat):
v = "float"
elif isinstance(value, t.Enum):
v = "choice"
elif isinstance(value, (t.Dict, t.List)):
v = "json"
elif isinstance(value, (t.Bool, t.StrBool)):
v = "boolean"
elif isinstance(value, DateTime):
v = "datetime"
else:
v = "string"
return name, v, extra
示例2: document_schema
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def document_schema():
choices = ['a', 'b', 'c']
schema = t.Dict({
t.Key('_id'): MongoId,
t.Key('title'): t.String(max_length=200),
t.Key('category'): t.String(max_length=200),
t.Key('body'): t.String,
t.Key('views'): t.ToInt,
t.Key('average_note'): t.ToFloat,
# t.Key('pictures'): t.Dict({}).allow_extra('*'),
t.Key('published_at'): DateTime,
# t.Key('tags'): t.List(t.Int),
t.Key('status'): t.Enum(*choices),
t.Key('visible'): t.ToBool,
})
return schema
示例3: test_list
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_list(self):
res = extract_error(t.List(t.ToInt), 1)
assert res == 'value is not a list'
res = t.List(t.ToInt).check([1, 2, 3])
assert res == [1, 2, 3]
res = t.List(t.String).check([u"foo", u"bar", u"spam"])
assert res == [u'foo', u'bar', u'spam']
res = extract_error(t.List(t.ToInt), [1, 2, 1 + 3j])
assert res == {2: 'value is not int'}
res = t.List(t.ToInt, min_length=1).check([1, 2, 3])
assert res == [1, 2, 3]
res = extract_error(t.List(t.ToInt, min_length=1), [])
assert res == 'list length is less than 1'
res = t.List(t.ToInt, max_length=2).check([1, 2])
assert res == [1, 2]
res = extract_error(t.List(t.ToInt, max_length=2), [1, 2, 3])
assert res == 'list length is greater than 2'
res = extract_error(t.List(t.ToInt), ["a"])
assert res == {0: "value can't be converted to int"}
示例4: test_dict
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_dict(self):
tt = construct({
'a': int,
'b': [str], # test List
'c': (str, str, 'atom string'), # test Tuple
'f': float, # test float
't': Decimal, # test Type
t.Key('k'): int, # test Key
})
assert tt({
'a': 5,
'b': [u'a'],
'c': [u'v', u'w', 'atom string'],
'f': 0.1,
't': Decimal('100'),
'k': 100,
}) == {
'a': 5,
'b': ['a'],
'c': (u'v', u'w', 'atom string'),
'f': 0.1,
't': Decimal('100'),
'k': 100,
}
示例5: mutate
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def mutate(cls, _, info, __):
logger.debug("agrs %s", info)
post_schema = t.Dict({
'title': t.String(min_length=2),
'user_id': t.String(min_length=2),
'content': t.String(min_length=2),
t.Key('tags',
optional=True): t.List(t.String,
min_length=1),
})
post_data = post_schema.check(info)
user_id = post_data.pop('user_id')
user = User.objects.get_or_404(id=user_id)
post = Post(author=user, **post_data)
post.save()
return cls(post=construct(PostField, post))
示例6: list_shared_vfolders
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def list_shared_vfolders(request: web.Request, params: Any) -> web.Response:
'''
List shared vfolders.
Not available for group vfolders.
'''
dbpool = request.app['dbpool']
access_key = request['keypair']['access_key']
target_vfid = params['vfolder_id']
log.info('VFOLDER.LIST_SHARED_VFOLDERS (ak:{})', access_key)
async with dbpool.acquire() as conn:
j = (vfolder_permissions
.join(vfolders, vfolders.c.id == vfolder_permissions.c.vfolder)
.join(users, users.c.uuid == vfolder_permissions.c.user))
query = (sa.select([vfolder_permissions,
vfolders.c.id, vfolders.c.name,
users.c.email])
.select_from(j)
.where((vfolders.c.user == request['user']['uuid'])))
if target_vfid is not None:
query = query.where(vfolders.c.id == target_vfid)
result = await conn.execute(query)
shared_list = await result.fetchall()
shared_info = []
for shared in shared_list:
shared_info.append({
'vfolder_id': str(shared.id),
'vfolder_name': str(shared.name),
'shared_by': request['user']['email'],
'shared_to': {
'uuid': str(shared.user),
'email': shared.email,
},
'perm': shared.permission.value,
})
resp = {'shared': shared_info}
return web.json_response(resp, status=200)
示例7: build_trafaret
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def build_trafaret(sa_type, **kwargs):
if isinstance(sa_type, sa.sql.sqltypes.Enum):
trafaret = t.Enum(*sa_type.enums, **kwargs)
# check for Text should be before String
elif isinstance(sa_type, sa.sql.sqltypes.Text):
trafaret = t.String(**kwargs)
elif isinstance(sa_type, sa.sql.sqltypes.String):
trafaret = t.String(max_length=sa_type.length, **kwargs)
elif isinstance(sa_type, sa.sql.sqltypes.Integer):
trafaret = t.ToInt(**kwargs)
elif isinstance(sa_type, sa.sql.sqltypes.Float):
trafaret = t.ToFloat(**kwargs)
elif isinstance(sa_type, sa.sql.sqltypes.DateTime):
trafaret = DateTime(**kwargs) # RFC3339
elif isinstance(sa_type, sa.sql.sqltypes.Date):
trafaret = DateTime(**kwargs) # RFC3339
elif isinstance(sa_type, sa.sql.sqltypes.Boolean):
trafaret = t.ToBool(**kwargs)
# Add PG related JSON and ARRAY
elif isinstance(sa_type, postgresql.JSON):
trafaret = AnyDict | t.List(AnyDict)
# Add PG related JSON and ARRAY
elif isinstance(sa_type, postgresql.ARRAY):
item_trafaret = build_trafaret(sa_type.item_type)
trafaret = t.List(item_trafaret)
else:
type_ = str(sa_type)
msg = 'Validator for type {} not implemented'.format(type_)
raise NotImplementedError(msg)
return trafaret
示例8: test_list
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_list():
trafaret = t.List(t.ToInt & check_int)
res = await (trafaret.async_check(['5']))
assert res == [5]
with pytest.raises(t.DataError) as res:
await trafaret.async_check(['5qwe'])
assert res.value.as_dict() == {0: "value can't be converted to int"}
示例9: test_forward
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_forward():
trafaret = t.Forward()
trafaret << t.List(t.ToInt & check_int)
res = await (trafaret.async_check(['5']))
assert res == [5]
示例10: test_list
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_list(self):
trafaret = t.List(CONTEXT_TRAFARET)
assert trafaret([123], context=123) == [123]
示例11: test_forward
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_forward(self):
trafaret = t.Forward()
trafaret << t.List(CONTEXT_TRAFARET)
assert trafaret([123], context=123) == [123]
示例12: test_forward
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_forward(self):
node = t.Forward()
res = extract_error(node, 'something')
assert res == 'trafaret not set yet'
node << t.Dict(name=t.String, children=t.List[node])
assert node.check({"name": u"foo", "children": []}) == {'children': [], 'name': u'foo'}
res = extract_error(node, {"name": u"foo", "children": [1]})
assert res == {'children': {0: 'value is not a dict'}}
res = node.check({"name": u"foo", "children": [{"name": u"bar", "children": []}]})
assert res == {'children': [{'children': [], 'name': u'bar'}], 'name': u'foo'}
with pytest.raises(RuntimeError): # __rshift__ is not overridden
node << t.Int()
示例13: test_list_meta
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_list_meta(self):
with pytest.raises(RuntimeError) as exc_info:
t.List[1:10]
assert exc_info.value.args[0] == 'Trafaret is required for List initialization'
示例14: test_2_0_regression
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_2_0_regression(self):
t_request = t.Dict({
t.Key('params', optional=True): t.Or(t.List(t.Any()), t.Mapping(t.AnyString(), t.Any())),
})
assert t_request.check({'params': {'aaa': 123}}) == {'params': {'aaa': 123}}
示例15: test_list_repr
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import List [as 别名]
def test_list_repr(self):
res = t.List(t.ToInt)
assert repr(res) == '<List(<ToInt>)>'
res = t.List(t.ToInt, min_length=1)
assert repr(res) == '<List(min_length=1 | <ToInt>)>'
res = t.List(t.ToInt, min_length=1, max_length=10)
assert repr(res) == '<List(min_length=1, max_length=10 | <ToInt>)>'
res = t.List[t.ToInt]
assert repr(res) == '<List(<ToInt>)>'
res = t.List[t.ToInt, 1:]
assert repr(res) == '<List(min_length=1 | <ToInt>)>'
res = t.List[:10, t.ToInt]
assert repr(res) == '<List(max_length=10 | <ToInt>)>'