Hook Manager
hook_manager
Classes:
-
HookManager–Manages hooks (
DGHooks) for aDGraph, supporting both shared and key-specific hooks.
HookManager
Manages hooks (DGHooks) for a DGraph, supporting both shared and key-specific hooks.
This class allows you to register hooks that modify or enrich batches of graph events
via transformation on the current, and optionally past history of the temporal graph.
The hook manager executed these transformation transparently to the user during data
iteration. Hooks can be shared across all keys or specific to a key.
Dependencies between hooks are automatically resolved using topological
sorting based on their requires and produces attributes.
Parameters:
-
keys(List[str]) –List of valid keys for key-specific hooks. Each key can have its own set of hooks, in addition to shared hooks.
Raises:
-
ValueError–If
keysis empty.
Methods:
-
activate–Context manager to temporarily set a key as active for hook execution.
-
execute_active_hooks–Executes all hooks (shared + key-specific) for the active key on a batch.
-
register–Registers a key-specific hook.
-
register_shared–Registers a shared hook that runs for all keys.
-
reset_state–Resets the internal state of all stateful hooks.
-
resolve_hooks–Resolves hook execution order by topologically sorting them based on dependencies.
-
set_active_hooks–Sets the currently active key for executing hooks.
Source code in tgm/hooks/hook_manager.py
38 39 40 41 42 43 44 45 46 | |
activate
Context manager to temporarily set a key as active for hook execution.
Parameters:
-
key(str) –The key to activate.
Source code in tgm/hooks/hook_manager.py
196 197 198 199 200 201 202 203 204 205 206 207 208 | |
execute_active_hooks
Executes all hooks (shared + key-specific) for the active key on a batch.
Parameters:
Returns:
-
DGBatch(DGBatch) –The modified batch after all hooks are applied.
Raises:
-
RuntimeError–If no active key is set.
-
UnresolvableHookDependenciesError–If required attributes are missing or hooks form a cyclic dependency.
Source code in tgm/hooks/hook_manager.py
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 | |
register
Registers a key-specific hook.
Parameters:
Raises:
-
KeyError–If
keyis not a declared key. -
BadHookProtocolError–If
hookdoes not implement theDGHookprotocol. -
RuntimeError–If called while a key is active.
Source code in tgm/hooks/hook_manager.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
register_shared
register_shared(hook: DGHook) -> None
Registers a shared hook that runs for all keys.
Parameters:
-
hook(DGHook) –The hook to register.
Raises:
-
BadHookProtocolError–If
hookdoes not implement theDGHookprotocol. -
RuntimeError–If called while a key is active.
Source code in tgm/hooks/hook_manager.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
reset_state
reset_state(key: str | None = None) -> None
Resets the internal state of all stateful hooks.
Parameters:
-
key(str | None, default:None) –If specified, resets only hooks for this key. Otherwise resets all keys and shared hooks.
Source code in tgm/hooks/hook_manager.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
resolve_hooks
resolve_hooks(key: str | None = None) -> None
Resolves hook execution order by topologically sorting them based on dependencies.
Parameters:
-
key(str | None, default:None) –If specified, resolves hooks only for this key. Otherwise resolves all keys.
Raises:
-
KeyError–If
keyis invalid. -
UnresolvableHookDependenciesError–If required attributes are missing or hooks form a cyclic dependency.
Source code in tgm/hooks/hook_manager.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
set_active_hooks
set_active_hooks(key: str) -> None
Sets the currently active key for executing hooks.
Parameters:
-
key(str) –The key to activate.
Raises:
-
KeyError–If
keyis not a declared key.
Source code in tgm/hooks/hook_manager.py
108 109 110 111 112 113 114 115 116 117 118 119 | |