graph
Handles creation and ops for DAGs
#
BaseGraph Objectsclass BaseGraph(ABC)
Class that holds graph methods
Attributes:
_dag
- graph of the dependencies between spock classes
#
__init__def __init__(dag: Dict, whoami: str)
Init call for Graph class
Arguments:
dag
- a directed acyclic graph as a dictionary (keys -> nodes, values -> edges)whoami
- str value of whom the caller is
#
dag@propertydef dag()
Returns the DAG
#
nodes@property@abstractmethoddef nodes()
Returns the nodes
#
node_names@propertydef node_names()
Returns the node names
#
node_map@propertydef node_map()
Returns a map of the node names to the underlying classes
#
reverse_map@propertydef reverse_map()
Returns a map from the underlying classes to the node names
#
roots@propertydef roots()
Returns the roots of the dependency graph
#
topological_order@propertydef topological_order()
Returns the topological sort of the DAG
#
_build@abstractmethoddef _build() -> Dict
Builds a dictionary of nodes and their edges (essentially builds the DAG)
Returns:
dictionary of nodes and their edges
#
_has_cyclesdef _has_cycles() -> bool
Uses DFS to check for cycles within the given graph
Returns:
boolean if a cycle is found
#
_cycle_dfsdef _cycle_dfs(node: str, visited: Dict, recursion_stack: Dict) -> bool
DFS via a recursion stack for cycles
Arguments:
node
- current graph node (spock class type)visited
- dictionary of visited nodesrecursion_stack
- dictionary that is the recursion stack that is used to find cycles
Returns:
boolean if a cycle is found
#
_topological_sortdef _topological_sort() -> List
Topologically sorts the DAG
Returns:
list of topological order
#
_topological_sort_dfsdef _topological_sort_dfs(node: str, visited: Dict, stack: List) -> None
Depth first search
Arguments:
node
- current nodevisited
- visited nodesstack
- order of graph
#
MergeGraph Objectsclass MergeGraph(BaseGraph)
Class that allows for merging of multiple graphs
Attributes:
_input_classes
- list of input classes that link to a backend_args
- variable nuber of graphs to merge_dag
- graph of the dependencies between spock classes
#
__init__def __init__(*args: Dict, *, input_classes: List)
Arguments:
*args
- variable number of graphs to mergeinput_classes
- list of input classes that link to a backend
#
_merge_inputs@staticmethoddef _merge_inputs(*args: Dict)
Merges multiple graphs into a single dependency graph
Arguments:
*args
- variable number of graphs to merge
Returns:
dictionary of the merged dependency graphs
#
nodes@propertydef nodes()
Returns the input_classes/nodes
#
_builddef _build() -> Dict
Builds a dictionary of nodes and their edges (essentially builds the DAG)
Returns:
dictionary of nodes and their edges
#
SelfGraph Objectsclass SelfGraph(BaseGraph)
#
node_map@propertydef node_map()
Returns a map of the node names to the underlying classes
#
reverse_map@propertydef reverse_map()
Returns a map from the underlying classes to the node names
#
resolvedef resolve() -> Tuple[Dict, Set]
Resolves variable references by searching thorough the current spock_space
Arguments:
Returns:
field dictionary containing the resolved values and a set containing all changed variables to delay casting post resolution
#
_builddef _build() -> Tuple[Dict, Dict]
Builds a dictionary of nodes and their edges (essentially builds the DAG)
Returns:
dictionary of nodes and their edges
#
VarGraph Objectsclass VarGraph(BaseGraph)
Class that helps with variable resolution by mapping dependencies
Attributes:
_input_classes
- list of input classes that link to a backend_cls_fields_tuple
- tuple of cls and the given field dict_dag
- graph of the dependencies between spock classesvar_resolver
- cls instance of the variable resolverref_map
- dictionary of the references that need to be mapped to a value
#
__init__def __init__(cls_fields_list: List[Tuple[_C, Dict]], input_classes: List)
Arguments:
cls_fields_list
- tuple of cls and the given field dictinput_classes
- list of input classes that link to a backend
#
cls_names@propertydef cls_names()
Returns the set of class names
#
cls_values@propertydef cls_values()
Returns a map of the class name and the underlying classes
#
cls_map@propertydef cls_map()
Returns a map between the class names and the field dictionaries
#
nodes@propertydef nodes()
Returns the input_classes/nodes
#
ref_2_resolve@propertydef ref_2_resolve() -> Set
Returns the values that need to be resolved
#
resolvedef resolve(spock_cls: str, spock_space: Dict) -> Tuple[Dict, Set]
Resolves variable references by searching thorough the current spock_space
Arguments:
spock_cls
- name of the spock classspock_space
- current spock_space to look for the underlying value
Returns:
field dictionary containing the resolved values and a set containing all changed variables to delay casting post resolution
#
_builddef _build() -> Tuple[Dict, Dict]
Builds a dictionary of nodes and their edges (essentially builds the DAG)
Returns:
tuple of dictionary of nodes and their edges and well as the dictionary map between the references
#
Graph Objectsclass Graph(BaseGraph)
Class that holds graph methods for determining dependencies between spock classes
Attributes:
_input_classes
- list of input classes that link to a backend_dag
- graph of the dependencies between spock classes_lazy
- attempts to lazily find @spock decorated classes registered within sys.modules["spock"].backend.config
#
__init__def __init__(input_classes: List, lazy: bool)
Init call for Graph class
Arguments:
input_classes
- list of input classes that link to a backendlazy
- attempts to lazily find @spock decorated classes registered within sys.modules["spock"].backend.config
#
nodes@propertydef nodes()
Returns the input_classes/nodes
#
_yield_class_deps@staticmethoddef _yield_class_deps(classes: Union[List, Tuple]) -> Generator[Tuple, None, None]
Generator to iterate through nodes and find dependencies
Arguments:
classes
- list or tuple of classes to iterate through
Yields:
tuple or the base input class and the current name of the dependent class
#
_lazily_find_classesdef _lazily_find_classes(classes: List) -> Tuple
Searches within the spock sys modules attributes to lazily find @spock decorated classes
These classes have been decorated with @spock but might not have been passes into the ConfigArgBuilder so this allows for 'lazy' lookup of these classes to make the call to ConfigArgBuilder a little less verbose when there are a lot of spock classes
Returns:
tuple of any lazily discovered classes
#
_lazily_find_parentsdef _lazily_find_parents() -> Tuple
Searches within the current set of input_classes (@spock decorated classes) to lazily find any parents
Given that lazy inheritance means that the parent classes won't be included (since they are cast to spock classes within the decorator and the MRO is handled internally) this allows the lazy flag to find those parent classes and add them to the SpockBuilder *args (input classes).
Returns:
tuple of any lazily discovered classes
#
_builddef _build() -> Dict
Builds a dictionary of nodes and their edges (essentially builds the DAG)
Returns:
dictionary of nodes and their edges