<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def applied( self, context: typing.Any = None, setenv: typing.Callable = operator.setitem, delenv: typing.Callable = None, getenv: typing.Callable = operator.getitem, ):
 """ Apply this environment to the context. If no context is supplied, os.environ is used. If you pass setenv= and delenv=, those will be used to apply the environment. delenv must not fail for non-existent environment variables. If context has a 'setenv' attribute, then we believe it's pytest's MonkeyPatch and use it accordingly. """

        if context is None:
            context = os.environ
        if hasattr(context, "setenv"):
            # context is probably pytest's MonkeyPatch
            setenv = context.setenv
            delenv = functools.partial(context.delenv, raising=False)
            getenv = functools.partial(operator.getitem, os.environ)
        elif delenv is None:
            setenv = functools.partial(operator.setitem, context)
            delenv = functools.partial(Environment._del_item, context)

        # Retain previous values so we can reset the changes we did
        previous_values = {}

        # Apply the values
        for k, v in self.items():
            try:
                previous_values[k] = getenv(k)
            except KeyError:
                previous_values[k] = None
            if v is None:
                delenv(k)
            else:
                setenv(k, v)

        try:
            yield self

        finally:
            # Set back the previous values
            for k, v in previous_values.items():
                if v is None:
                    delenv(k)
                else:
                    setenv(k, v)