How to use custom_hook method in Slash

Best Python code snippet using slash

singleton_utils.py

Source:singleton_utils.py Github

copy

Full Screen

1"""2Easy-to-use methods for getting the singleton SessionHook.3Sample usage:4import smdebug.(pytorch | tensorflow | mxnet) as smd5hook = smd.hook()6"""7# Standard Library8import atexit9# First Party10from smdebug.core.logger import get_logger11logger = get_logger()12_ts_hook = None13def _create_hook(json_config_path, hook_class):14 from smdebug.core.hook import BaseHook # prevent circular imports15 if not issubclass(hook_class, BaseHook):16 raise TypeError("hook_class needs to be a subclass of BaseHook", hook_class)17 # Either returns a hook or None18 try:19 hook = hook_class.create_from_json_file(json_file_path=json_config_path)20 set_hook(custom_hook=hook)21 except FileNotFoundError:22 pass23def get_hook(*, json_config_path: str, hook_class, create_if_not_exists: bool) -> "Hook":24 """Return a singleton SessionHook or None.25 If the singleton hook exists, we return it. No questions asked, `json_config_path` is a no-op.26 Otherwise return create_from_json_file().27 """28 global _ts_hook29 # Cannot create a hook if hook_class is not passed; invalid to call isinstance(obj, None).30 if hook_class is None:31 if create_if_not_exists:32 raise ValueError("Cannot create hook because hook_class is None")33 return _ts_hook34 # If hooks exists, either return or reset it based on hook_class35 if _ts_hook:36 # Return the hook if it exists and is the right type37 if isinstance(_ts_hook, hook_class):38 return _ts_hook39 # Reset the hook if it is the wrong type (user runs Session, then Estimator in same script).40 else:41 logger.info(f"Current hook is not instance of {hook_class}, so clearing it.")42 _ts_hook = None43 # Create if the user desires44 if create_if_not_exists and not _ts_hook:45 _create_hook(json_config_path, hook_class)46 return _ts_hook47def set_hook(custom_hook: "BaseHook") -> None:48 """Overwrite the current hook with the passed hook."""49 from smdebug.core.hook import BaseHook # prevent circular imports50 if not isinstance(custom_hook, BaseHook):51 raise TypeError(f"custom_hook={custom_hook} must be type BaseHook")52 global _ts_hook53 _ts_hook = custom_hook54 atexit.register(del_hook)55def del_hook() -> None:56 """ Set the hook singleton to None. """57 global _ts_hook...

Full Screen

Full Screen

hook.py

Source:hook.py Github

copy

Full Screen

1"""Hook framework in dataduct.2To make a function hookable, add the hook decorator like so:3@hook('hook_name')4def function():5 ...6"""7import os8import imp9import sys10from .helpers import parse_path11def default_before_hook(*args, **kwargs):12 """The default before hook, will act like it's not even there13 """14 return args, kwargs15def default_after_hook(result):16 """The default after hook, will act like it's not even there17 """18 return result19def get_hooks(hook_name):20 """Returns the before hook and after hook (in a tuple) for a particular21 hook name22 """23 from dataduct.config import Config24 config = Config()25 if 'HOOKS_BASE_PATH' not in config.etl:26 return default_before_hook, default_after_hook27 hook_file = parse_path(hook_name + '.py', 'HOOKS_BASE_PATH')28 if not os.path.isfile(hook_file):29 return default_before_hook, default_after_hook30 # Delete the previous custom hook, so the imports are not merged.31 if 'custom_hook' in sys.modules:32 del sys.modules['custom_hook']33 # Get the hook functions, falling back to the default hooks34 custom_hook = imp.load_source('custom_hook', hook_file)35 before_hook = getattr(custom_hook, 'before_hook', default_before_hook)36 after_hook = getattr(custom_hook, 'after_hook', default_after_hook)37 return before_hook, after_hook38def hook(hook_name):39 """The hook decorator creator40 """41 before_hook, after_hook = get_hooks(hook_name)42 def hook_decorator(func):43 """The hook decorator44 """45 def function_wrapper(*args, **kwargs):46 """The hook wrapper for the function47 """48 new_args, new_kwargs = before_hook(*args, **kwargs)49 result = func(*new_args, **new_kwargs)50 new_result = after_hook(result)51 return new_result52 return function_wrapper...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful