notice

This is unreleased documentation for Rasa Documentation Main/Unreleased version.
For the latest released documentation, see the latest version (3.x).

Version: Main/Unreleased

rasa.shared.utils.common

class_from_module_path

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

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. RasaException, in case the imported result is something other than a class

all_subclasses

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

Returns all known (imported) subclasses of a class.

module_path_from_instance

def module_path_from_instance(inst: Any) -> Text

Return the module path of an instance's class.

sort_list_of_dicts_by_first_key

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

Sorts a list of dictionaries by their first key.

lazy_property

def 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

def 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

def 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

def 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

def mark_as_experimental_feature(feature_name: Text) -> None

Warns users that they are using an experimental feature.

arguments_of

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

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

extract_duplicates

def extract_duplicates(list1: List[Any], list2: List[Any]) -> List[Any]

Extracts duplicates from two lists.

clean_duplicates

def clean_duplicates(dupes: Dict[Text, Any]) -> Dict[Text, Any]

Removes keys for empty values.

merge_dicts

def merge_dicts(tempDict1: Dict[Text, Any],
tempDict2: Dict[Text, Any],
override_existing_values: bool = False) -> Dict[Text, Any]

Merges two dicts.

merge_lists

def merge_lists(list1: List[Any],
list2: List[Any],
override: bool = False) -> List[Any]

Merges two lists.

merge_lists_of_dicts

def merge_lists_of_dicts(dict_list1: List[Dict],
dict_list2: List[Dict],
override_existing_values: bool = False) -> List[Dict]

Merges two dict lists.