ls_mlkit.util.decorators module

ls_mlkit.util.decorators.cache_to_disk(root_datadir='cached_dataset', exclude_first_arg=False)[source]

Cache the result of a function to disk

Parameters:
  • root_datadir (str, optional) – the root directory to save the cached data. Defaults to “cached_dataset”.

  • exclude_first_arg (bool, optional) – whether to exclude the first argument of the function when generating the cache filename. Defaults to False.

ls_mlkit.util.decorators.class_decorator(decorate_dict: Dict[str, List[Callable]] = {})[source]
ls_mlkit.util.decorators.inherit_docstring_from_parent(method_name: str = None)[source]

Method decorator that inherits docstring from a specific parent class method.

This decorator allows you to explicitly inherit a docstring from a parent class method, even if the method names are different.

Usage:

class ChildClass(ParentClass):
    @inherit_docstring_from_parent('parent_method_name')
    def child_method(self):
        pass

    @inherit_docstring_from_parent()  # Uses same method name
    def some_method(self):
        pass
Parameters:

method_name – Name of the parent method to inherit docstring from. If None, uses the decorated method’s name.

Returns:

The decorated method with inherited docstring

ls_mlkit.util.decorators.inherit_docstrings(cls)[source]

Class decorator that automatically inherits docstrings from parent class methods.

This decorator will: 1. Find methods in the class that don’t have docstrings 2. Look for the same method in parent classes 3. Copy the docstring from the first parent class that has one 4. Handle methods marked with @inherit_docstring_from_parent

Usage:

@inherit_docstrings
class ChildClass(ParentClass):
    def some_method(self):
        # This method will inherit docstring from ParentClass.some_method
        pass

    @inherit_docstring_from_parent('parent_method')
    def child_method(self):
        # This method will inherit docstring from ParentClass.parent_method
        pass
Parameters:

cls – The class to apply docstring inheritance to

Returns:

The modified class with inherited docstrings

ls_mlkit.util.decorators.register_class_to_dict(cls=None, *, key_name=None, global_dict=None)[source]

Register a class to a global dictionary

Parameters:
  • cls (class, optional) – the class to register. Defaults to None.

  • key_name (str, optional) – the name of the key to register the class. Defaults to None.

  • global_dict (dict, optional) – the global dictionary to register the class. Defaults to None.

ls_mlkit.util.decorators.require_keys(*required_keys)[source]

Decorator to ensure returned dictionary contains required keys

ls_mlkit.util.decorators.timer(format='ms')[source]

Timer the execution time of a function

Parameters:

format (str, optional) – the format of the execution time. Defaults to “ms”.

ls_mlkit.util.decorators.wandb_logger()[source]

Log the result of a function to wandb