本文整理汇总了Python中trafaret.Float方法的典型用法代码示例。如果您正苦于以下问题:Python trafaret.Float方法的具体用法?Python trafaret.Float怎么用?Python trafaret.Float使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trafaret
的用法示例。
在下文中一共展示了trafaret.Float方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_valid
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import Float [as 别名]
def test_is_valid(self):
string_trafaret = t.Float()
assert string_trafaret.is_valid(1.5) == True
assert string_trafaret.is_valid('foo') == False
示例2: construct
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import Float [as 别名]
def construct(arg):
'''
Shortcut syntax to define trafarets.
- int, str, float and bool will return t.Int, t.String, t.Float and t.Bool
- one element list will return t.List
- tuple or list with several args will return t.Tuple
- dict will return t.Dict. If key has '?' at the and it will be optional and '?' will be removed
- any callable will be t.Call
- otherwise it will be returned as is
construct is recursive and will try construct all lists, tuples and dicts args
'''
if isinstance(arg, t.Trafaret):
return arg
elif isinstance(arg, tuple) or (isinstance(arg, list) and len(arg) > 1):
return t.Tuple(*(construct(a) for a in arg))
elif isinstance(arg, list):
# if len(arg) == 1
return t.List(construct(arg[0]))
elif isinstance(arg, dict):
return t.Dict({construct_key(key): construct(value) for key, value in arg.items()})
elif isinstance(arg, str):
return t.Atom(arg)
elif isinstance(arg, type):
if arg is int:
return t.ToInt()
elif arg is float:
return t.ToFloat()
elif arg is str:
return t.String()
elif arg is bool:
return t.Bool()
else:
return t.Type(arg)
elif callable(arg):
return t.Call(arg)
else:
return arg
示例3: __init__
# 需要导入模块: import trafaret [as 别名]
# 或者: from trafaret import Float [as 别名]
def __init__(self, allow_extra):
self.schema = t.Dict({
'id': t.Int(),
'client_name': t.String(max_length=255),
'sort_index': t.Float,
# t.Key('client_email', optional=True): t.Or(t.Null | t.Email()),
t.Key('client_phone', optional=True): t.Or(t.Null | t.String(max_length=255)),
t.Key('location', optional=True): t.Or(t.Null | t.Dict({
'latitude': t.Or(t.Float | t.Null),
'longitude': t.Or(t.Float | t.Null),
})),
t.Key('contractor', optional=True): t.Or(t.Null | t.Int(gt=0)),
t.Key('upstream_http_referrer', optional=True): t.Or(t.Null | t.String(max_length=1023)),
t.Key('grecaptcha_response'): t.String(min_length=20, max_length=1000),
t.Key('last_updated', optional=True): t.Or(t.Null | t.String >> parse),
t.Key('skills', default=[]): t.List(t.Dict({
'subject': t.String,
'subject_id': t.Int,
'category': t.String,
'qual_level': t.String,
'qual_level_id': t.Int,
t.Key('qual_level_ranking', default=0): t.Float,
})),
})
if allow_extra:
self.schema.allow_extra('*')