Python marshmallow.fields.Method() Examples

The following are 5 code examples of marshmallow.fields.Method(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module marshmallow.fields , or try the search function .
Example #1
Source File: schema.py    From packit with MIT License 7 votes vote down vote up
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 
Example #2
Source File: schema.py    From packit with MIT License 6 votes vote down vote up
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 
Example #3
Source File: utils_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #4
Source File: test_jit.py    From toasted-marshmallow with Apache License 2.0 5 votes vote down vote up
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() 
Example #5
Source File: jit.py    From toasted-marshmallow with Apache License 2.0 5 votes vote down vote up
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