railtracks.context

1from .operations import (
2    delete,
3    get,
4    keys,
5    put,
6    update,
7)
8
9__all__ = ["put", "get", "update", "delete", "keys"]
def put(key: str, value: Any) -> None:
39def put(
40    key: str,
41    value: Any,
42) -> None:
43    """
44    Set a value in the context.
45
46    Args:
47        key (str): The key to set.
48        value (Any): The value to set.
49    """
50    context = safe_get_runner_context()
51    context.external_context.put(key, value)
52    record_put(key, value)

Set a value in the context.

Arguments:
  • key (str): The key to set.
  • value (Any): The value to set.
def get(key: str, /, default: Optional[Any] = None) -> Any:
16def get(
17    key: str,
18    /,
19    default: Any | None = None,
20) -> Any:
21    """
22    Get a value from context
23
24    Args:
25        key (str): The key to retrieve.
26        default (Any | None): The default value to return if the key does not exist. If set to None and the key does not exist, a KeyError will be raised.
27    Returns:
28        Any: The value associated with the key, or the default value if the key does not exist.
29
30    Raises:
31        KeyError: If the key does not exist and no default value is provided.
32    """
33    context = safe_get_runner_context()
34    value = context.external_context.get(key, default=default)
35    record_get(key, value)
36    return value

Get a value from context

Arguments:
  • key (str): The key to retrieve.
  • default (Any | None): The default value to return if the key does not exist. If set to None and the key does not exist, a KeyError will be raised.
Returns:

Any: The value associated with the key, or the default value if the key does not exist.

Raises:
  • KeyError: If the key does not exist and no default value is provided.
def update(data: dict[str, typing.Any]) -> None:
55def update(data: dict[str, Any]) -> None:
56    """
57    Sets the values in the context. If the context already has values, this will overwrite them, but it will not delete any existing keys.
58
59    Args:
60        data (dict[str, Any]): The data to update the context with.
61    """
62    data = dict(data)
63    context = safe_get_runner_context()
64    context.external_context.update(data)
65    record_update(data)

Sets the values in the context. If the context already has values, this will overwrite them, but it will not delete any existing keys.

Arguments:
  • data (dict[str, Any]): The data to update the context with.
def delete(key: str) -> None:
68def delete(key: str) -> None:
69    """
70    Delete a key from the context.
71
72    Args:
73        key (str): The key to delete.
74
75    Raises:
76        KeyError: If the key does not exist.
77    """
78    context = safe_get_runner_context()
79    context.external_context.delete(key)
80    record_delete(key)

Delete a key from the context.

Arguments:
  • key (str): The key to delete.
Raises:
  • KeyError: If the key does not exist.
def keys() -> KeysView[str]:
83def keys() -> KeysView[str]:
84    """
85    Get the keys of the context.
86
87    Returns:
88        KeysView[str]: The keys in the context.
89    """
90    context = safe_get_runner_context()
91    return context.external_context.keys()

Get the keys of the context.

Returns:

KeysView[str]: The keys in the context.