Storables¶
Contains utilities for creating storable types and to write and read from disk.
- dman.core.storables.register_storable(name: str, cls: Type[Any], *, write: Optional[Callable[[Any, Optional[BaseContext]], Any]] = None, read: Optional[Callable[[Any, Optional[BaseContext]], Any]] = None)[source]¶
Register a class as a storable type with a given name
- dman.core.storables.get_custom_storable(cls: Type[Any], default=None)[source]¶
Get the signature of a custom storable object.
- dman.core.storables.storable(cls=None, /, *, name: Optional[str] = None)[source]¶
Make a class storable.
Returns the same class as was passed in and the class is registered as a storable type. Write and Read methods are added automatically if cls is a dataclass or a serializable type. Otherwise a
__write__
and__read__
method should be provided for storing. If these are not provided conversion will fail.See Defining Storables for detailed examples on how to create storable types.
- Parameters:
cls – The class to convert.
name (str, optional) – The name of the storable. Defaults to the name of the class.
- Raises:
ValueError – The class could not be made storable. Provide a
__write__
and__read__
method.
- exception dman.core.storables.WriteException[source]¶
Exception raised when a write of a storable fails.
- exception dman.core.storables.ReadException[source]¶
Exception raised when a read of a storable fails.
- dman.core.storables.write(storable, path: PathLike, context: Optional[BaseContext] = None)[source]¶
Write a storable to the specified path.
See Defining Storables for details on how to use this method.
- Parameters:
storable – The storable instance to write
path (os.PathLike) – Target path
context (BaseContext, optional) – Context to pass to the
__write__
method. Defaults to None.
- dman.core.storables.read(type: Union[str, Type], path: PathLike, context: Optional[BaseContext] = None, **kwargs)[source]¶
Read a storable from the specified path.
See Defining Storables for details on how to use this method.
- Parameters:
type (Union[str, Type]) – The type (string) of the storable
path (os.PathLike) – The path from which to read
context (BaseContext, optional) – The context to pass to the
__read__
method. Defaults to None.