results_manager
results_manager ¶
AsyncFileBackend ¶
AsyncFileBackend(
base_dir: Union[str, Path],
create_if_missing: bool = True,
locks_dir: Optional[Union[str, Path]] = None,
)
Bases: AsyncResultsBackend[T]
Async wrapper for FileBackend.
Runs the synchronous FileBackend methods in a threadpool to avoid blocking the event loop.
Initialize the AsyncFileBackend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dir
|
Union[str, Path]
|
Base directory to store results |
required |
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist |
True
|
locks_dir
|
Optional[Union[str, Path]]
|
Directory to store lock files. If None, uses a system temp directory. |
None
|
Source code in src/results_manager/async_backends/file_backend.py
clear
async
¶
delete
async
¶
Asynchronously delete a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if deleted, False if not found |
Source code in src/results_manager/async_backends/file_backend.py
exists
async
¶
Asynchronously check if a result exists for the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the result exists, False otherwise |
Source code in src/results_manager/async_backends/file_backend.py
get
async
¶
get(
result_id: Union[str, List[str]],
model_class: Optional[Type[T]] = None,
namespace: Optional[str] = None,
) -> T
Asynchronously retrieve a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
model_class
|
Optional[Type[T]]
|
Optional model class to validate against |
None
|
namespace
|
Optional[str]
|
Optional namespace to look in |
None
|
Returns:
Type | Description |
---|---|
T
|
Pydantic model instance |
Source code in src/results_manager/async_backends/file_backend.py
list_ids
async
¶
Asynchronously list all result IDs, optionally filtered by a prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
Union[str, List[str]]
|
Optional prefix path to filter results |
None
|
Returns:
Type | Description |
---|---|
List[str]
|
List of result IDs |
Source code in src/results_manager/async_backends/file_backend.py
set
async
¶
set(
result_id: Union[str, List[str]],
data: BaseModel,
behavior: SetBehavior = RAISE_IF_EXISTS,
namespace: Optional[str] = None,
strict_namespace: bool = False,
) -> bool
Asynchronously store a result with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
data
|
BaseModel
|
Pydantic model instance to store |
required |
behavior
|
SetBehavior
|
How to handle existing data with the same ID |
RAISE_IF_EXISTS
|
namespace
|
Optional[str]
|
Optional namespace to store the model in |
None
|
strict_namespace
|
bool
|
If True, raises an error if model is in multiple namespaces |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if data was written, False if skipped |
Source code in src/results_manager/async_backends/file_backend.py
AsyncResultsBackend ¶
Abstract base class for async results storage backends.
Implementations should provide asynchronous storage and retrieval of Pydantic models.
clear
abstractmethod
async
¶
delete
abstractmethod
async
¶
exists
abstractmethod
async
¶
Asynchronously check if a result exists for the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the result exists, False otherwise |
Source code in src/results_manager/async_backends/base.py
get
abstractmethod
async
¶
get(
result_id: Union[str, List[str]],
model_class: Optional[Type[T]] = None,
namespace: Optional[str] = None,
) -> T
Asynchronously retrieve a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
model_class
|
Optional[Type[T]]
|
Optional model class to validate against |
None
|
namespace
|
Optional[str]
|
Optional namespace to look in |
None
|
Returns:
Type | Description |
---|---|
T
|
Pydantic model instance |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the result doesn't exist |
ValueError
|
If the model type is not registered |
Source code in src/results_manager/async_backends/base.py
list_ids
abstractmethod
async
¶
set
abstractmethod
async
¶
set(
result_id: Union[str, List[str]],
data: BaseModel,
behavior: SetBehavior = RAISE_IF_EXISTS,
namespace: Optional[str] = None,
strict_namespace: bool = False,
) -> bool
Asynchronously store a result with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
data
|
BaseModel
|
Pydantic model instance to store |
required |
behavior
|
SetBehavior
|
How to handle existing data with the same ID |
RAISE_IF_EXISTS
|
namespace
|
Optional[str]
|
Optional namespace to store the model in |
None
|
strict_namespace
|
bool
|
If True, raises an error if model is in multiple namespaces |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if data was written, False if skipped |
Raises:
Type | Description |
---|---|
FileExistsError
|
If data already exists (for RAISE_IF_EXISTS) or if different data exists (for RAISE_IF_DIFFERENT) |
Source code in src/results_manager/async_backends/base.py
AsyncResultsManager ¶
AsyncResultsManager(
base_dir: Union[str, Path] = None,
create_if_missing: bool = True,
backend: Optional[AsyncResultsBackend] = None,
)
Bases: Generic[T]
Async version of ResultsManager for managing results from parallel processes.
Provides an asynchronous interface for storing and retrieving pydantic models.
Initialize the AsyncResultsManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dir
|
Union[str, Path]
|
Base directory for file storage (used only if backend is None) |
None
|
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist |
True
|
backend
|
Optional[AsyncResultsBackend]
|
Optional custom async backend to use. If None, uses AsyncFileBackend. |
None
|
Source code in src/results_manager/async_manager.py
clear
async
¶
delete
async
¶
exists
async
¶
Asynchronously check if a result exists for the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the result exists, False otherwise |
Source code in src/results_manager/async_manager.py
get
async
¶
get(
result_id: Union[str, List[str]],
model_class: Optional[Type[T]] = None,
namespace: Optional[str] = None,
) -> T
Asynchronously retrieve a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
model_class
|
Optional[Type[T]]
|
Optional model class to validate against |
None
|
namespace
|
Optional[str]
|
Optional namespace to look in |
None
|
Returns:
Type | Description |
---|---|
T
|
Pydantic model instance |
Source code in src/results_manager/async_manager.py
list_ids
async
¶
set
async
¶
set(
result_id: Union[str, List[str]],
data: BaseModel,
behavior: SetBehavior = RAISE_IF_EXISTS,
namespace: Optional[str] = None,
strict_namespace: bool = False,
) -> bool
Asynchronously store a result with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
data
|
BaseModel
|
Pydantic model instance to store |
required |
behavior
|
SetBehavior
|
How to handle existing data with the same ID |
RAISE_IF_EXISTS
|
namespace
|
Optional[str]
|
Optional namespace to store the model in |
None
|
strict_namespace
|
bool
|
If True, raises an error if model is in multiple namespaces |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if data was written, False if skipped |
Source code in src/results_manager/async_manager.py
FileBackend ¶
FileBackend(
base_dir: Union[str, Path],
create_if_missing: bool = True,
locks_dir: Optional[Union[str, Path]] = None,
)
Bases: ResultsBackend[T]
File-based implementation of ResultsBackend.
Stores results as JSON files in a hierarchical directory structure.
Initialize the FileBackend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dir
|
Union[str, Path]
|
Base directory to store results |
required |
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist |
True
|
locks_dir
|
Optional[Union[str, Path]]
|
Directory to store lock files. If None, uses a system temp directory. |
None
|
Source code in src/results_manager/backends/file_backend.py
clear ¶
Clear all stored results.
Source code in src/results_manager/backends/file_backend.py
delete ¶
Delete a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if deleted, False if not found |
Source code in src/results_manager/backends/file_backend.py
exists ¶
Check if a result exists for the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the result exists, False otherwise |
Source code in src/results_manager/backends/file_backend.py
get ¶
get(
result_id: Union[str, List[str]],
model_class: Optional[Type[T]] = None,
namespace: Optional[str] = None,
) -> T
Retrieve a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
model_class
|
Optional[Type[T]]
|
Optional model class to validate against. If not provided, the stored model type will be used. |
None
|
namespace
|
Optional[str]
|
Optional namespace override to look for the model in |
None
|
Returns:
Type | Description |
---|---|
T
|
Pydantic model instance |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the result doesn't exist |
ValueError
|
If the model type is not registered |
ValidationError
|
If the data doesn't match the model schema |
Source code in src/results_manager/backends/file_backend.py
list_ids ¶
List all result IDs, optionally filtered by a prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
Union[str, List[str]]
|
Optional prefix path to filter results |
None
|
Returns:
Type | Description |
---|---|
List[str]
|
List of result IDs |
Source code in src/results_manager/backends/file_backend.py
set ¶
set(
result_id: Union[str, List[str]],
data: BaseModel,
behavior: SetBehavior = RAISE_IF_EXISTS,
namespace: Optional[str] = None,
strict_namespace: bool = False,
) -> bool
Store a result with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
data
|
BaseModel
|
Pydantic model instance to store |
required |
behavior
|
SetBehavior
|
How to handle existing data with the same ID |
RAISE_IF_EXISTS
|
namespace
|
Optional[str]
|
Optional namespace to store the model in |
None
|
strict_namespace
|
bool
|
If True, raises an error if model is in multiple namespaces |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if data was written, False if skipped |
Source code in src/results_manager/backends/file_backend.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
ResultsBackend ¶
Abstract base class for results storage backends.
Implementations should provide storage and retrieval of Pydantic models based on unique IDs.
clear
abstractmethod
¶
delete
abstractmethod
¶
exists
abstractmethod
¶
Check if a result exists for the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the result exists, False otherwise |
Source code in src/results_manager/backends/base.py
get
abstractmethod
¶
get(
result_id: Union[str, List[str]],
model_class: Optional[Type[T]] = None,
namespace: Optional[str] = None,
) -> T
Retrieve a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
model_class
|
Optional[Type[T]]
|
Optional model class to validate against |
None
|
namespace
|
Optional[str]
|
Optional namespace to look in |
None
|
Returns:
Type | Description |
---|---|
T
|
Pydantic model instance |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the result doesn't exist |
ValueError
|
If the model type is not registered |
Source code in src/results_manager/backends/base.py
list_ids
abstractmethod
¶
set
abstractmethod
¶
set(
result_id: Union[str, List[str]],
data: BaseModel,
behavior: SetBehavior = RAISE_IF_EXISTS,
namespace: Optional[str] = None,
strict_namespace: bool = False,
) -> bool
Store a result with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
data
|
BaseModel
|
Pydantic model instance to store |
required |
behavior
|
SetBehavior
|
How to handle existing data with the same ID |
RAISE_IF_EXISTS
|
namespace
|
Optional[str]
|
Optional namespace to store the model in |
None
|
strict_namespace
|
bool
|
If True, raises an error if model is in multiple namespaces |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if data was written, False if skipped |
Raises:
Type | Description |
---|---|
FileExistsError
|
If data already exists (for RAISE_IF_EXISTS) or if different data exists (for RAISE_IF_DIFFERENT) |
Source code in src/results_manager/backends/base.py
ResultsManager ¶
ResultsManager(
base_dir: Union[str, Path] = None,
create_if_missing: bool = True,
backend: Optional[ResultsBackend] = None,
)
Bases: Generic[T]
Manages results from parallel processes, storing and retrieving pydantic models.
This class provides a unified interface to different storage backends.
Initialize the ResultsManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_dir
|
Union[str, Path]
|
Base directory for file storage (used only if backend is None) |
None
|
create_if_missing
|
bool
|
Whether to create the directory if it doesn't exist |
True
|
backend
|
Optional[ResultsBackend]
|
Optional custom backend to use. If None, uses FileBackend. |
None
|
Source code in src/results_manager/manager.py
clear ¶
delete ¶
exists ¶
get ¶
get(
result_id: Union[str, List[str]],
model_class: Optional[Type[T]] = None,
namespace: Optional[str] = None,
) -> T
Retrieve a result by ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
model_class
|
Optional[Type[T]]
|
Optional model class to validate against. If not provided, the stored model type will be used. |
None
|
namespace
|
Optional[str]
|
Optional namespace override to look for the model in |
None
|
Returns:
Type | Description |
---|---|
T
|
Pydantic model instance |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the result doesn't exist |
ValueError
|
If the model type is not registered |
ValidationError
|
If the data doesn't match the model schema |
Source code in src/results_manager/manager.py
list_ids ¶
set ¶
set(
result_id: Union[str, List[str]],
data: BaseModel,
behavior: SetBehavior = RAISE_IF_EXISTS,
namespace: Optional[str] = None,
strict_namespace: bool = False,
) -> bool
Store a result with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_id
|
Union[str, List[str]]
|
Unique identifier or hierarchical path for the result |
required |
data
|
BaseModel
|
Pydantic model instance to store |
required |
behavior
|
SetBehavior
|
How to handle existing data with the same ID |
RAISE_IF_EXISTS
|
namespace
|
Optional[str]
|
Optional namespace to store the model in. If None, will try to determine the namespace from the model class automatically. |
None
|
strict_namespace
|
bool
|
If True, raises an error if the model is registered in multiple non-default namespaces |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if data was written, False if skipped (only for SKIP_IF_EXISTS) |
Raises:
Type | Description |
---|---|
FileExistsError
|
If data already exists (for RAISE_IF_EXISTS) or if different data exists (for RAISE_IF_DIFFERENT) |
Source code in src/results_manager/manager.py
clear_registry ¶
Clear the model registry, optionally only for a specific namespace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
namespace
|
Optional[str]
|
If provided, only clear this namespace. Otherwise, clear all. |
None
|
Source code in src/results_manager/model_registry.py
find_model_in_all_namespaces ¶
Find a model by name in all namespaces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The name of the model class |
required |
Returns:
Type | Description |
---|---|
List[Tuple[str, Type[BaseModel]]]
|
List of (namespace, model_class) tuples for all matches |
Source code in src/results_manager/model_registry.py
find_model_namespace ¶
Find the namespace for a model class.
If the model is registered in multiple namespaces, behavior depends on the 'strict' parameter: - If strict=False (default): Prioritizes non-default namespaces, returns the first one found - If strict=True: Raises ValueError if found in multiple non-default namespaces
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
Type[BaseModel]
|
The model class to find the namespace for |
required |
strict
|
bool
|
Whether to raise an error if the model is in multiple non-default namespaces |
False
|
Returns:
Type | Description |
---|---|
Optional[str]
|
The namespace name if found, None otherwise |
Raises:
Type | Description |
---|---|
ValueError
|
If strict=True and the model is registered in multiple non-default namespaces |
Source code in src/results_manager/model_registry.py
get_model_class ¶
Retrieve a model class from the registry by name and namespace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The name of the model class |
required |
namespace
|
str
|
The namespace to look in |
DEFAULT_NAMESPACE
|
Returns:
Type | Description |
---|---|
Optional[Type[BaseModel]]
|
The model class if found, None otherwise |
Source code in src/results_manager/model_registry.py
get_models_in_namespace ¶
get_namespaces ¶
register_model ¶
Register a pydantic model class in the registry.
Can be used as a decorator with or without arguments:
@register_model class MyModel(BaseModel): ...
@register_model(namespace="custom") class MyModel(BaseModel): ...
Or programmatically: register_model(MyModel, namespace="custom")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class_or_namespace
|
Any
|
The model class to register or a namespace string |
None
|
namespace
|
str
|
The namespace to register the model in (when used programmatically) |
DEFAULT_NAMESPACE
|
Returns:
Type | Description |
---|---|
The decorator function or the registered model class |