def normalize_context()

in pythonflow/core.py [0:0]


    def normalize_context(self, context, **kwargs):
        """
        Normalize a context by replacing all operation names with operation instances.

        .. note::
           This function modifies the context in place. Use :code:`context=context.copy()` to avoid
           the context being modified.

        Parameters
        ----------
        context : dict[Operation or str, object]
            Context whose keys are operation instances or names.
        kwargs : dict[str, object]
            Additional context information keyed by variable name.

        Returns
        -------
        normalized_context : dict[Operation, object]
            Normalized context whose keys are operation instances.

        Raises
        ------
        ValueError
            If the context specifies more than one value for any operation.
        ValueError
            If `context` is not a mapping.
        """
        if context is None:
            context = {}
        elif not isinstance(context, collections.abc.Mapping):
            raise ValueError("`context` must be a mapping.")

        operations = list(context)
        for operation in operations:
            value = context.pop(operation)
            operation = self.normalize_operation(operation)
            if operation in context:
                raise ValueError(f"duplicate value for operation '{operation}'")
            context[operation] = value

        # Add the keyword arguments
        for name, value in kwargs.items():
            operation = self.operations[name]
            if operation in context:
                raise ValueError(f"duplicate value for operation '{operation}'")
            context[operation] = value

        return context