Skip to content

context.py

Provides request context for dependency injection.

Context

Bases: UserDict

Dictionary-like object that stores request context for dependency injection.

Source code in jetweb/context.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Context(UserDict):
    """
    Dictionary-like object that stores request context for dependency injection.
    """

    def params_for(self, function: Callable) -> dict:
        """
        Extract only the parameters from context that a function expects.

        :param function: Callable whose signature will be inspected.
        :returns: Dictionary of context values relevant to the function.
        """
        signature = inspect.signature(function).parameters
        return {
            name: value
            for name, value in {"context": self, **self.data}.items()
            if name in signature
        }

params_for(function)

Extract only the parameters from context that a function expects.

Parameters:

Name Type Description Default
function Callable

Callable whose signature will be inspected.

required

Returns:

Type Description
dict

Dictionary of context values relevant to the function.

Source code in jetweb/context.py
15
16
17
18
19
20
21
22
23
24
25
26
27
def params_for(self, function: Callable) -> dict:
    """
    Extract only the parameters from context that a function expects.

    :param function: Callable whose signature will be inspected.
    :returns: Dictionary of context values relevant to the function.
    """
    signature = inspect.signature(function).parameters
    return {
        name: value
        for name, value in {"context": self, **self.data}.items()
        if name in signature
    }