def __init__(self, name, fname=None, field=None):
"""Initialize filter."""
self.name = name
self.fname = fname or name
self.field = field or fields.Raw(attribute=name)
python类Raw()的实例源码
def make_paginated_list_schema_class(cls, ns, item_schema):
"""
Generate a schema class that represents a paginted list of items.
"""
class PaginatedListSchema(Schema):
__alias__ = "{}_list".format(ns.subject_name)
items = fields.List(fields.Nested(item_schema), required=True)
_links = fields.Raw()
return PaginatedListSchema
def make_paginated_list_schema_class(cls, ns, item_schema):
class PaginatedListSchema(Schema):
__alias__ = "{}_list".format(ns.subject_name)
offset = fields.Integer(required=True)
limit = fields.Integer(required=True)
count = fields.Integer(required=True)
items = fields.List(fields.Nested(item_schema), required=True)
_links = fields.Raw()
@property
def csv_column_order(self):
return getattr(item_schema, "csv_column_order", None)
return PaginatedListSchema
def field_to_dynamo_type(field):
"""Given a marshmallow field object return the appropriate Dynamo type character"""
if isinstance(field, fields.Raw):
return 'B'
if isinstance(field, fields.Number):
return 'N'
return 'S'
def convert_default(self, field, **params):
"""Return raw field."""
ma_field = self.TYPE_MAPPING.get(type(field), fields.Raw)
return ma_field(**params)
def __filter_fields(self, field_names, obj, many=False):
"""Return only those field_name:field_obj pairs specified by
``field_names``.
:param set field_names: Field names to include in the final
return dictionary.
:returns: An dict of field_name:field_obj pairs.
"""
if obj and many:
try: # Homogeneous collection
# Prefer getitem over iter to prevent breaking serialization
# of objects for which iter will modify position in the collection
# e.g. Pymongo cursors
if hasattr(obj, '__getitem__') and callable(getattr(obj, '__getitem__')):
obj_prototype = obj[0]
else:
obj_prototype = next(iter(obj))
except (StopIteration, IndexError): # Nothing to serialize
return self.declared_fields
obj = obj_prototype
ret = self.dict_class()
for key in field_names:
if key in self.declared_fields:
ret[key] = self.declared_fields[key]
else: # Implicit field creation (class Meta 'fields' or 'additional')
if obj:
attribute_type = None
try:
if isinstance(obj, Mapping):
attribute_type = type(obj[key])
else:
attribute_type = type(getattr(obj, key))
except (AttributeError, KeyError) as err:
err_type = type(err)
raise err_type(
'"{0}" is not a valid field for {1}.'.format(key, obj))
field_obj = self.TYPE_MAPPING.get(attribute_type, fields.Field)()
else: # Object is None
field_obj = fields.Field()
# map key -> field (default to Raw)
ret[key] = field_obj
return ret