Skip to content

Debug

load_workspace(path, target_globals=None)

Load variables from a joblib file into the specified global namespace.

Parameters:

Name Type Description Default
path str

Path to the .joblib file containing the saved workspace.

required
target_globals Optional[Dict[str, Any]]

Dictionary (typically globals()) into which variables are loaded. Defaults to the caller's global namespace.

None
Example

load_workspace("workspace.joblib")

Source code in isca_tools/utils/debug.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def load_workspace(path: str, target_globals: Optional[Dict[str, Any]] = None) -> None:
    """Load variables from a joblib file into the specified global namespace.

    Args:
        path (str): Path to the `.joblib` file containing the saved workspace.
        target_globals (Optional[Dict[str, Any]]):
            Dictionary (typically `globals()`) into which variables are loaded.
            Defaults to the caller's global namespace.

    Example:
        >>> load_workspace("workspace.joblib")
    """
    # Expand '~' and normalize path
    load_path = os.path.expanduser(path)

    # Get the caller's globals if none provided
    if target_globals is None:
        frame = inspect.currentframe().f_back
        target_globals = frame.f_globals

    # Load dictionary of saved variables
    data = load(load_path)

    if not isinstance(data, dict):
        raise ValueError("Joblib file does not contain a dictionary of variables.")

    # Inject into globals
    for name, value in data.items():
        target_globals[name] = value

save_workspace(filename, variables=None, compress=('lz4', 3))

Save selected or all variables from the current workspace to a joblib file.

If no variables are specified, saves all local variables from the caller's scope.

Parameters:

Name Type Description Default
filename str

Path to the .joblib file to save.

required
variables Optional[Union[Dict[str, Any], list[str]]]

Either: - A dictionary of variable names to values to save, or - A list of variable names (as strings) to extract from the caller's workspace. If None, all variables from the caller's local scope are saved.

None
compress Union[bool, str, tuple]

Compression method for joblib. Examples: - False (no compression) - 'lz4' or 'gzip' - ('lz4', 3) for method + compression level

('lz4', 3)
Example

x, y = 1, [1, 2, 3] save_workspace("test.joblib", ["x", "y"]) save_workspace("all_vars.joblib") # saves everything in current workspace

Source code in isca_tools/utils/debug.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def save_workspace(
    filename: str,
    variables: Optional[Union[Dict[str, Any], list[str]]] = None,
    compress: Union[bool, str, tuple] = ("lz4", 3)
) -> None:
    """Save selected or all variables from the current workspace to a joblib file.

    If no variables are specified, saves all local variables from the caller's scope.

    Args:
        filename (str): Path to the `.joblib` file to save.
        variables (Optional[Union[Dict[str, Any], list[str]]]):
            Either:
            - A dictionary of variable names to values to save, or
            - A list of variable names (as strings) to extract from the caller's workspace.
            If None, all variables from the caller's local scope are saved.
        compress (Union[bool, str, tuple]):
            Compression method for joblib.
            Examples:
                - False (no compression)
                - 'lz4' or 'gzip'
                - ('lz4', 3) for method + compression level

    Example:
        >>> x, y = 1, [1, 2, 3]
        >>> save_workspace("test.joblib", ["x", "y"])
        >>> save_workspace("all_vars.joblib")  # saves everything in current workspace
    """
    # Get caller's frame
    frame = inspect.currentframe().f_back

    # If no variables provided, grab everything from local scope
    if variables is None:
        variables_to_save = frame.f_locals.copy()
    elif isinstance(variables, list):
        # Extract listed variable names from caller's local scope
        variables_to_save = {name: frame.f_locals[name] for name in variables if name in frame.f_locals}
    elif isinstance(variables, dict):
        variables_to_save = variables
    else:
        raise TypeError("`variables` must be a dict, list of variable names, or None")

    dump(variables_to_save, filename, compress=compress)