notice

This is documentation for Rasa Documentation v2.x, which is no longer actively maintained.
For up-to-date documentation, see the latest version (3.x).

Version: 2.x

rasa.shared.utils.common

class_from_module_path

class_from_module_path(module_path: Text, lookup_path: Optional[Text] = None) -> Any

Given the module name and path of a class, tries to retrieve the class.

The loaded class can be used to instantiate new objects.

Arguments:

  • module_path - either an absolute path to a Python class, or the name of the class in the local / global scope.
  • lookup_path - a path where to load the class from, if it cannot be found in the local / global scope.

Returns:

a Python class

Raises:

ImportError, in case the Python class cannot be found.

all_subclasses

all_subclasses(cls: Any) -> List[Any]

Returns all known (imported) subclasses of a class.

module_path_from_instance

module_path_from_instance(inst: Any) -> Text

Return the module path of an instance's class.

sort_list_of_dicts_by_first_key

sort_list_of_dicts_by_first_key(dicts: List[Dict]) -> List[Dict]

Sorts a list of dictionaries by their first key.

lazy_property

lazy_property(function: Callable) -> Any

Allows to avoid recomputing a property over and over.

The result gets stored in a local var. Computation of the property will happen once, on the first call of the property. All succeeding calls will use the value stored in the private property.

cached_method

cached_method(f: Callable[..., Any]) -> Callable[..., Any]

Caches method calls based on the call's args and kwargs.

Works for async and sync methods. Don't apply this to functions.

Arguments:

  • f - The decorated method whose return value should be cached.

Returns:

The return value which the method gives for the first call with the given arguments.

transform_collection_to_sentence

transform_collection_to_sentence(collection: Collection[Text]) -> Text

Transforms e.g. a list like ['A', 'B', 'C'] into a sentence 'A, B and C'.

minimal_kwargs

minimal_kwargs(kwargs: Dict[Text, Any], func: Callable, excluded_keys: Optional[List] = None) -> Dict[Text, Any]

Returns only the kwargs which are required by a function. Keys, contained in the exception list, are not included.

Arguments:

  • kwargs - All available kwargs.
  • func - The function which should be called.
  • excluded_keys - Keys to exclude from the result.

Returns:

Subset of kwargs which are accepted by func.

mark_as_experimental_feature

mark_as_experimental_feature(feature_name: Text) -> None

Warns users that they are using an experimental feature.

arguments_of

arguments_of(func: Callable) -> List[Text]

Return the parameters of the function func as a list of names.