本文整理匯總了Python中marshmallow.fields.Method方法的典型用法代碼示例。如果您正苦於以下問題:Python fields.Method方法的具體用法?Python fields.Method怎麽用?Python fields.Method使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類marshmallow.fields
的用法示例。
在下文中一共展示了fields.Method方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: spec_source_id_fm
# 需要導入模塊: from marshmallow import fields [as 別名]
# 或者: from marshmallow.fields import Method [as 別名]
def spec_source_id_fm(value: Union[str, int]):
"""
method used in spec_source_id field.Method
If value is int, it is transformed int -> "Source" + str(int)
ex.
1 -> "Source1"
:return str: prepends "Source" in case input value is int
"""
if value:
try:
value = int(value)
except ValueError:
# not a number
pass
else:
# is a number!
value = f"Source{value}"
return value
示例2: specfile_path_pre
# 需要導入模塊: from marshmallow import fields [as 別名]
# 或者: from marshmallow.fields import Method [as 別名]
def specfile_path_pre(self, data: Dict, **kwargs):
"""
Method for pre-processing specfile_path value.
Set it to downstream_package_name if specified, else leave unset.
:param data: conf dictionary to process
:return: processed dictionary
"""
if not data: # data is None when .packit.yaml is empty
return data
specfile_path = data.get("specfile_path", None)
if not specfile_path:
downstream_package_name = data.get("downstream_package_name", None)
if downstream_package_name:
data["specfile_path"] = f"{downstream_package_name}.spec"
logger.debug(
f'Setting `specfile_path` to "./{downstream_package_name}.spec".'
)
else:
# guess it?
logger.debug(
"Neither `specfile_path` nor `downstream_package_name` set."
)
return data
示例3: test_map_type_dump_ma_method_returns_fr_raw
# 需要導入模塊: from marshmallow import fields [as 別名]
# 或者: from marshmallow.fields import Method [as 別名]
def test_map_type_dump_ma_method_returns_fr_raw():
class TestSchema(Schema):
method_field = ma.Method()
TestApi = Api()
expected_method_field_mapping = fr.Raw
map_result = utils.map_type(TestSchema, TestApi, 'TestSchema','dump')
assert isinstance(map_result['method_field'], expected_method_field_mapping)
示例4: schema
# 需要導入模塊: from marshmallow import fields [as 別名]
# 或者: from marshmallow.fields import Method [as 別名]
def schema():
class BasicSchema(Schema):
class Meta:
ordered = True
foo = fields.Integer(attribute='@#')
bar = fields.String()
raz = fields.Method('raz_')
meh = fields.String(load_only=True)
blargh = fields.Boolean()
def raz_(self, obj):
return 'Hello!'
return BasicSchema()
示例5: _should_skip_field
# 需要導入模塊: from marshmallow import fields [as 別名]
# 或者: from marshmallow.fields import Method [as 別名]
def _should_skip_field(field_name, field_obj, context):
# type: (str, fields.Field, JitContext) -> bool
load_only = getattr(field_obj, 'load_only', False)
dump_only = getattr(field_obj, 'dump_only', False)
# Marshmallow 2.x.x doesn't properly set load_only or
# dump_only on Method objects. This is fixed in 3.0.0
# https://github.com/marshmallow-code/marshmallow/commit/1b676dd36cbb5cf040da4f5f6d43b0430684325c
if isinstance(field_obj, fields.Method):
load_only = (
bool(field_obj.deserialize_method_name) and
not bool(field_obj.serialize_method_name)
)
dump_only = (
bool(field_obj.serialize_method_name) and
not bool(field_obj.deserialize_method_name)
)
if load_only and context.is_serializing:
return True
if dump_only and not context.is_serializing:
return True
if context.only and field_name not in context.only:
return True
if context.exclude and field_name in context.exclude:
return True
return False