本文整理汇总了Python中trafaret.String方法的典型用法代码示例。如果您正苦于以下问题:Python trafaret.String方法的具体用法?Python trafaret.String怎么用?Python trafaret.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trafaret
的用法示例。
在下文中一共展示了trafaret.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: text_filter
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def text_filter(query, value, schema):
string_columns = [s.name for s in schema.keys
if isinstance(s.trafaret, t.String)]
query_list = []
for column_name in string_columns:
query_list.append(op(defaultdict(lambda: {}),
column_name,
"like",
value))
query["$or"] = query_list
return query
# TODO: use functional style to create query
# do not modify dict inside functions, modify dict on
# same level
示例2: build_field
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [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
示例3: sa_table
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def sa_table():
choices = ['a', 'b', 'c']
meta = sa.MetaData()
post = sa.Table(
'test_post', meta,
sa.Column('id', sa.Integer, nullable=False),
sa.Column('title', sa.String(200), nullable=False),
sa.Column('category', sa.String(200), nullable=True),
sa.Column('body', sa.Text, nullable=False),
sa.Column('views', sa.Integer, nullable=False),
sa.Column('average_note', sa.Float, nullable=False),
# sa.Column('pictures', postgresql.JSON, server_default='{}'),
sa.Column('published_at', sa.DateTime, nullable=False),
# sa.Column('tags', postgresql.ARRAY(sa.Integer), server_default='{}'),
sa.Column('status',
sa.Enum(*choices, name="enum_name", native_enum=False),
server_default="a", nullable=False),
sa.Column('visible', sa.Boolean, nullable=False),
# Indexes #
sa.PrimaryKeyConstraint('id', name='post_id_pkey'))
return post
示例4: test_mapping
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_mapping():
trafaret = t.Mapping(t.String, t.ToInt & check_int)
res = await (trafaret.async_check({'a': '5'}))
assert res == {'a': 5}
# bad key
with pytest.raises(t.DataError) as res:
await (trafaret.async_check({None: '5'}))
assert res.value.as_dict() == {None: {"key": "value is not a string"}}
# bad value
with pytest.raises(t.DataError) as res:
await (trafaret.async_check({'b': 'qwe'}))
assert res.value.as_dict() == {'b': {"value": "value can't be converted to int"}}
# is not a dict
with pytest.raises(t.DataError) as res:
await (trafaret.async_check(None))
assert res.value.as_dict() == "value is not a dict"
示例5: test_base
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_base(self):
trafaret = t.Dict(foo=t.ToInt, bar=t.String)
trafaret.check({"foo": 1, "bar": u"spam"})
res = t.extract_error(trafaret, {"foo": 1, "bar": 2})
assert res == {'bar': 'value is not a string'}
res = extract_error(trafaret, {"foo": 1})
assert res == {'bar': 'is required'}
res = extract_error(trafaret, {"foo": 1, "bar": u"spam", "eggs": None})
assert res == {'eggs': 'eggs is not allowed key'}
trafaret = trafaret.allow_extra("eggs")
trafaret.check({"foo": 1, "bar": u"spam", "eggs": None})
trafaret.check({"foo": 1, "bar": u"spam"})
res = extract_error(trafaret, {"foo": 1, "bar": u"spam", "ham": 100})
assert res == {'ham': 'ham is not allowed key'}
trafaret = trafaret.allow_extra("*")
trafaret = trafaret.ignore_extra("a")
trafaret.check({"foo": 1, "bar": u"spam", "ham": 100})
trafaret.check({"foo": 1, "bar": u"spam", "ham": 100, "baz": None})
res = extract_error(trafaret, {"foo": 1, "ham": 100, "baz": None})
assert res == {'bar': 'is required'}
示例6: test_base3
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_base3(self):
trafaret = t.Dict({t.Key('bar', default=u'nyanya') >> 'baz': t.String}, foo=t.ToInt)
res = trafaret.check({'foo': 4})
assert res == {'baz': u'nyanya', 'foo': 4}
trafaret = trafaret.allow_extra('*')
res = extract_error(trafaret, {'baz': u'spam', 'foo': 4})
assert res == {'baz': 'baz key was shadowed'}
trafaret = trafaret.allow_extra('*', trafaret=t.String)
res = extract_error(trafaret, {'baaz': 5, 'foo': 4})
assert res == {'baaz': 'value is not a string'}
res = trafaret({'baaz': u'strstr', 'foo':4})
assert res == {'baaz': u'strstr', 'foo':4, 'baz': u'nyanya'}
trafaret = trafaret.ignore_extra('fooz')
res = trafaret.check({'foo': 4, 'fooz': 5})
assert res == {'baz': u'nyanya', 'foo': 4}
trafaret = trafaret.ignore_extra('*')
res = trafaret.check({'foo': 4, 'foor': 5})
assert res == {'baz': u'nyanya', 'foo': 4}
示例7: test_list
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [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"}
示例8: test_string
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_string(self):
res = t.String().check(u"foo")
assert res == u'foo'
res = extract_error(t.String(), u"")
assert res == 'blank value is not allowed'
res = t.String(allow_blank=True).check(u"")
assert res == u''
res = extract_error(t.String(), 1)
assert res == 'value is not a string'
res = t.String(min_length=2, max_length=3).check(u'123')
assert res == u'123'
res = extract_error(t.String(min_length=2, max_length=6), u'1')
assert res == 'String is shorter than 2 characters'
res = extract_error(t.String(min_length=2, max_length=6), u'1234567')
assert res == 'String is longer than 6 characters'
with pytest.raises(AssertionError) as exc_info:
t.String(min_length=2, max_length=6, allow_blank=True)
assert exc_info.value.args[0] == 'Either allow_blank or min_length should be specified, not both'
res = t.String(min_length=0, max_length=6, allow_blank=True).check(u'123')
assert res == u'123'
示例9: test_xor_key
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_xor_key(self):
trafaret = t.Dict(xor_key('name', 'nick', t.String()))
res = trafaret({'name': u'Nickolay'})
assert res == {'name': u'Nickolay'}
res = catch_error(trafaret, {'name': u'Nickolay', 'nick': u'Sveta'})
assert res.as_dict() == {
'name': 'correct only if nick is not defined',
'nick': 'correct only if name is not defined',
}
res = catch_error(trafaret, {})
assert res.as_dict() == {
'name': 'is required if nick is not defined',
'nick': 'is required if name is not defined',
}
示例10: mutate
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [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))
示例11: build_trafaret
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [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
示例12: text_filter
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def text_filter(query, value, table):
pairs = ((n, c) for n, c in table.c.items()
if isinstance(c.type, sa.sql.sqltypes.String))
sub_queries = []
for name, column in pairs:
do_compare = op("like", column)
sub_queries.append(do_compare(column, value))
query = query.where(or_(*sub_queries))
return query
# TODO: validate that value supplied in filter has same type as in table
# TODO: use functional style to create query
示例13: test_dict_extra_and_ignore
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_dict_extra_and_ignore():
trafaret = t.Dict(
t.Key('a', to_name='A', trafaret=t.String),
allow_extra=['one_extra'],
allow_extra_trafaret=t.String,
ignore_extra=['one_ignore'],
)
res = await (trafaret.async_check({'a': 's', 'one_extra': 's', 'one_ignore': 's'}))
assert res == {'A': 's', 'one_extra': 's'}
# extra key that is not declared in extra not in ignore
with pytest.raises(t.DataError) as res:
await trafaret.async_check({'a': 's', 'bad_extra': 's'})
assert res.value.as_dict() == {'bad_extra': 'bad_extra is not allowed key'}
# extra key that was shadowed
with pytest.raises(t.DataError) as res:
await trafaret.async_check({'a': 's', 'A': 's'})
assert res.value.as_dict() == {'A': 'A key was shadowed'}
# extra key with failed check
with pytest.raises(t.DataError) as res:
await trafaret.async_check({'a': 's', 'one_extra': 5})
assert res.value.as_dict() == {'one_extra': 'value is not a string'}
# shadowing with allow_extra='*'
trafaret = trafaret.allow_extra('*')
with pytest.raises(t.DataError) as res:
await trafaret.async_check({'a': 's', 'A': 's'})
assert res.value.as_dict() == {'A': 'A key was shadowed'}
示例14: test_repr
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_repr(self):
trafaret = t.Dict(foo=t.ToInt, bar=t.String, allow_extra=['eggs'])
assert repr(trafaret) == '<Dict(extras=(eggs) | <Key "bar" <String>>, <Key "foo" <ToInt>>)>'
trafaret = trafaret.allow_extra('*')
assert repr(trafaret) == '<Dict(any, extras=(eggs) | <Key "bar" <String>>, <Key "foo" <ToInt>>)>'
trafaret = trafaret.ignore_extra("a")
assert repr(trafaret) == '<Dict(any, ignore=(a), extras=(eggs) | <Key "bar" <String>>, <Key "foo" <ToInt>>)>'
示例15: test_add_kwargs_ignore_any
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import String [as 别名]
def test_add_kwargs_ignore_any(self):
first = t.Dict(
t.Key('bip', trafaret=t.String()), ignore_extra='*'
)
second = t.Dict(
t.Key('bop', trafaret=t.Int())
)
third = first + second
third.check({'bip': u'bam', 'bop': 17, 'matter': False})
assert third.ignore_any