Skip to main content

graph

Handles creation and ops for DAGs

BaseGraph Objects#

class 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_cycles#

def _has_cycles() -> bool

Uses DFS to check for cycles within the given graph

Returns:

boolean if a cycle is found

_cycle_dfs#

def _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 nodes
  • recursion_stack - dictionary that is the recursion stack that is used to find cycles

Returns:

boolean if a cycle is found

_topological_sort#

def _topological_sort() -> List

Topologically sorts the DAG

Returns:

list of topological order

_topological_sort_dfs#

def _topological_sort_dfs(node: str, visited: Dict, stack: List) -> None

Depth first search

Arguments:

  • node - current node
  • visited - visited nodes
  • stack - order of graph

MergeGraph Objects#

class 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 merge
  • input_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

_build#

def _build() -> Dict

Builds a dictionary of nodes and their edges (essentially builds the DAG)

Returns:

dictionary of nodes and their edges

SelfGraph Objects#

class 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

resolve#

def 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

_build#

def _build() -> Tuple[Dict, Dict]

Builds a dictionary of nodes and their edges (essentially builds the DAG)

Returns:

dictionary of nodes and their edges

VarGraph Objects#

class 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 classes
  • var_resolver - cls instance of the variable resolver
  • ref_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 dict
  • input_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

resolve#

def 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 class
  • spock_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

_build#

def _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 Objects#

class 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 backend
  • lazy - 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_classes#

def _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_parents#

def _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

_build#

def _build() -> Dict

Builds a dictionary of nodes and their edges (essentially builds the DAG)

Returns:

dictionary of nodes and their edges