本文整理匯總了Python中schematics.Model方法的典型用法代碼示例。如果您正苦於以下問題:Python schematics.Model方法的具體用法?Python schematics.Model怎麽用?Python schematics.Model使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類schematics
的用法示例。
在下文中一共展示了schematics.Model方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: schematics_model_merge
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def schematics_model_merge(a: Type[Model], *others: Tuple[Type[Model]]):
"""
Merge multi schematics model
:param a:
:param others:
:return:
"""
class MergedModel(Model):
pass
for name, field in a._fields.items():
MergedModel._append_field(name, field)
for i in others:
for name, field in i._fields.items():
MergedModel._append_field(name, field)
return MergedModel
示例2: schematics_model_to_json_schema
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def schematics_model_to_json_schema(model: Type[Model], generate_required=True):
required = []
properties = {}
for name, field in model._fields.items():
field: BaseType
properties[name] = schematics_field_to_schema(field)
if field.required: required.append(name)
ret = {
"type": "object",
"properties": properties
}
if generate_required:
ret['required'] = required
return ret
示例3: test_json_any
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def test_json_any():
class MyModel(Model):
a = JSONType()
b = FloatType()
a = MyModel({'a': {
'a': 'b'
}})
a.validate()
a = MyModel({'a': '{'})
a.validate()
a = MyModel({'a': json.dumps('{')})
a.validate()
a = MyModel({'a': [1, 2, 3]})
a.validate()
a = MyModel({'a': json.dumps([1, 2, 3]), 'b': '1'})
a.validate()
print(type(a.a))
assert isinstance(a.a, list)
示例4: _fetch_fields
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def _fetch_fields(cls_or_self):
"""
4 values must be set up in this function:
1. cls.table_name: str
2. cls.primary_key: str
3. cls.data_model: Type[schematics.Model]
4. cls.foreign_keys: Dict['column', List[SQLForeignKey]]
:param cls_or_self:
:return:
"""
pass
示例5: view_validate_check
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def view_validate_check(view: "BaseView", va_query: Optional[Type[Model]], va_post: Optional[Type[Model]],
va_headers: Optional[Type[Model]] = None, va_write_value: Type[Model] = None):
with ErrorCatchContext(view):
def do_validate(va_model, data, err_cls):
try:
return va_model(strict=False, validate=True, partial=False, raw_data=data)
except DataError as e:
raise err_cls(e.to_primitive())
if va_query:
# TODO: 這裏有問題,對SQL請求來說,多個同名參數項,會在實際解析時會被折疊為一個數組,但是這裏沒有
view._.validated_query = do_validate(va_query, view.params, InvalidParams)
if view.method in BaseRequest.POST_METHODS:
from slim.base._view.abstract_sql_view import AbstractSQLView
post_data = await view.post_data()
if va_write_value:
if isinstance(view, AbstractSQLView):
write_values = []
if view.current_interface == InnerInterfaceName.BULK_INSERT:
items = post_data.get('items')
if isinstance(items, (List, Tuple)):
for i in items:
write_values.append(do_validate(va_write_value, i, InvalidPostData))
else:
raise InvalidPostData("`items` from post data should be list")
elif view.current_interface in (InnerInterfaceName.SET, InnerInterfaceName.NEW):
write_values.append(do_validate(va_write_value, post_data, InvalidPostData))
view._.validated_write_values = write_values
if va_post:
view._.validated_post = do_validate(va_post, await view.post_data(), InvalidPostData)
if va_headers:
view._.validated_headers = do_validate(va_headers, view.headers, InvalidHeaders)
示例6: append_validate
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def append_validate(va_query: Type[Model] = None, va_post: Type[Model] = None, va_write_value: Type[Model] = None,
va_headers: Type[Model] = None):
"""
:param va_query:
:param va_post:
:param va_write_value:
:param va_headers
:return:
"""
def _(func):
assert asyncio.iscoroutinefunction(func), 'Must be async function.'
@functools.wraps(func)
async def inner(view: 'AbstractSQLView', *args, **kwargs):
await view_validate_check(view, va_query, va_post, va_headers, va_write_value)
if view.is_finished: return
return await func(view, *args, **kwargs)
meta = _decorator_fix(func, inner)
if va_query:
meta.va_query_lst.append(va_query)
if va_post:
meta.va_post_lst.append(va_post)
if va_write_value:
meta.va_write_value_lst.append(va_write_value)
return inner
return _
示例7: test_json_dict
# 需要導入模塊: import schematics [as 別名]
# 或者: from schematics import Model [as 別名]
def test_json_dict():
class MyModel(Model):
a = JSONDictType(StringType)
a = MyModel({'a': {
'a': 'b'
}})
a.validate()
a = MyModel({'a': json.dumps({
'a': 'b'
})})
a.validate()