tlo.dependencies module
Functions for getting, checking and sorting dependencies of Module subclasses.
- exception ModuleDependencyError
Bases:
ExceptionRaised when a module dependency is missing or there are circular dependencies.
- exception MultipleModuleInstanceError
Bases:
ExceptionRaised when multiple instances of the same module are registered.
- get_init_dependencies(module: Module | Type[Module], module_names_present: Set[str]) Set[str]
Get the initialisation dependencies for a
Modulesubclass.- Parameters:
module –
Modulesubclass to get dependencies for.module_names_present – Set of names of
Modulesubclasses that will be present in simulation to use to select optional initialisation dependencies.
- Returns:
Set of
Modulesubclass names corresponding to initialisation dependencies ofmodule, including any optional dependencies present.
- get_all_dependencies(module: Module | Type[Module], module_names_present: Set[str]) Set[str]
Get all dependencies for a
Modulesubclass.- Parameters:
module –
Modulesubclass to get dependencies for.module_names_present – Set of names of
Modulesubclasses that will be present in simulation to use to select optional initialisation dependencies.
- Returns:
Set of
Modulesubclass names corresponding to dependencies ofmodule, including any optional dependencies present.
- get_missing_dependencies(module_instances: ~typing.Iterable[~tlo.core.Module], get_dependencies: ~typing.Callable[[~tlo.core.Module | ~typing.Type[~tlo.core.Module], ~typing.Set[str]], ~typing.Set[str]] = <function get_all_dependencies>) Set[str]
Get the set of missing required dependencies if any from an iterable of modules.
- Parameters:
module_instances – Iterable of
Modulesubclass instances to get missing dependencies for.get_dependencies – Callable which extracts the set of dependencies to check for from a module instance. Defaults to extracting all dependencies.
- Returns:
Set of
Modulesubclass names corresponding to missing dependencies.
- initialise_missing_dependencies(modules: Iterable[Module], **module_kwargs) Set[Module]
Get list of initialised instances of any missing dependencies for an iterable of modules.
- Parameters:
modules – Iterable of
Modulesubclass instances to get instances of missing dependencies for.module_kwargs – Any keyword arguments to use when initialising missing module dependencies.
- Returns:
Set of
Modulesubclass instances corresponding to missing dependencies.
- get_all_required_dependencies(module: Module | Type[Module], module_names_present: Set[str] | None = None) Set[str]
Get all non-optional dependencies for a
Modulesubclass.- Parameters:
module –
Modulesubclass to get dependencies for.module_names_present – Set of names of
Modulesubclasses that will be present in simulation to use to select optional initialisation dependencies. Unused by this function, but kept as an argument to ensure a consistent interface with the other dependency-getter functions.
- Returns:
Set of
Modulesubclass names corresponding to non-optional dependencies ofmodule.
- topologically_sort_modules(module_instances: ~typing.Iterable[~tlo.core.Module], get_dependencies: ~typing.Callable[[~tlo.core.Module | ~typing.Type[~tlo.core.Module], ~typing.Set[str]], ~typing.Set[str]] = <function get_init_dependencies>) Generator[Module, None, None]
Generator which yields topological sort of modules based on their dependencies.
A topological sort of a dependency graph is ordered such that any dependencies of a node in the graph are guaranteed to be yielded before the node itself. This implementation uses a depth-first search algorithm (https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search).
- Parameters:
module_instances – The set of module instances to topologically sort. The yielded module instances will consist of all nodes in this set which must include instances of their (recursive) dependencies.
get_dependencies – Function which given a module gets the set of module dependencies. Defaults to returing the
Module.INIT_DEPENDENCIESclass attribute.
- Raises:
ModuleDependencyError – Raised when a module dependency is missing from
module_instancesor a module has circular dependencies.MultipleModuleInstanceError – Raised when multiple instances of the same module are passed in
module_instances.
- Returns:
Generator which yields module instances in topologically sorted order.
- is_valid_tlo_module_subclass(obj: Any, excluded_modules: Set[str]) bool
Determine whether object is a
Modulesubclass and not in an excluded set.- Parameters:
obj – Object to check if
Modulesubclass.excluded_modules – Set of names of
Modulesubclasses to force check to returnFalsefor.
- Returns:
Trueisobjis a _strict_ subclass ofModuleand not in theexcluded_modulesset.
- get_module_class_map(excluded_modules: Set[str]) Mapping[str, Type[Module]]
Constructs a map from
Modulesubclass names to class objects.- Parameters:
excluded_modules – Set of
Modulesubclass names to exclude from map.- Returns:
A mapping from unqualified
Modulesubclass to names to the correponding class objects. This adds an implicit requirement that the names of all theModulesubclasses are unique.- Raises:
RuntimError – Raised if multiple
Modulesubclasses with the same name are defined (and not included in theexclude_modulesset).
- get_dependencies_and_initialise(*module_classes: ~typing.Type[~tlo.core.Module], module_class_map: ~typing.Mapping[str, ~typing.Type[~tlo.core.Module]], excluded_module_classes: ~typing.Set[~tlo.core.Module] | None = None, get_dependencies: ~typing.Callable[[~tlo.core.Module | ~typing.Type[~tlo.core.Module], ~typing.Set[str]], ~typing.Set[str]] = <function get_init_dependencies>, **module_class_kwargs) Generator[Module, None, None]
Generate a sequence of
Moduleinstances including all dependencies.The generated sequence of initialised
Modulesubclass instances will correspond to all the (recursive) dependencies of the seedModulesubclasses inmodule_classes.- Parameters:
module_classes –
Modulesubclass(es) to seed dependency search with.module_class_map – Mapping from
Modulesubclass names to classes.excluded_module_classes – Any
Modulesubclasses to not yield instances of in the returned generator.get_dependencies – Function which given a module gets the set of module dependencies. Defaults to returing the
Module.INIT_DEPENDENCIESclass attribute.module_class_kwargs – Any keyword arguments to pass to initialisers for
Modulesubclasses if present in their__init__method signature.
- Returns:
Sequence of initialised
Modulesubclass instances corresponding to all of theModulesubclasses and their the (recursive) dependencies in the seedmodule_classes.
- check_dependencies_present(module_instances: ~typing.Iterable[~tlo.core.Module], get_dependencies: ~typing.Callable[[~tlo.core.Module | ~typing.Type[~tlo.core.Module], ~typing.Set[str]], ~typing.Set[str]] = <function get_all_dependencies>)
Check whether an iterable of modules contains the required dependencies.
- Parameters:
module_instances – Iterable of
Modulesubclass instances to check.get_dependencies – Callable which extracts the set of dependencies to check for from a module instance. Defaults to extracting all dependencies.
- Raises:
ModuleDependencyError – Raised if any dependencies are missing.