krrood.utils#

Attributes#

T

Exceptions#

DataclassException

A base exception class for dataclass-based exceptions.

Functions#

recursive_subclasses(→ List[Type[T]])

get_full_class_name(cls)

Returns the full name of a class, including the module name.

inheritance_path_length(→ Optional[int])

Calculate the inheritance path length between two classes.

module_and_class_name(→ str)

get_default_value(dataclass_type, field_name)

Return the default value for a given field in a dataclass.

get_default_values_for_dataclass(dataclass_type)

Return a dict mapping field names to their default values.

Module Contents#

krrood.utils.T#
krrood.utils.recursive_subclasses(cls: Type[T]) List[Type[T]]#
Parameters:

cls – The class.

Returns:

A list of the classes subclasses without the class itself.

exception krrood.utils.DataclassException#

Bases: Exception

A base exception class for dataclass-based exceptions. The way this is used is by inheriting from it and setting the message field in the __post_init__ method, then calling the super().__post_init__() method.

message: str = None#
krrood.utils.get_full_class_name(cls)#

Returns the full name of a class, including the module name.

Parameters:

cls – The class.

Returns:

The full name of the class

krrood.utils.inheritance_path_length(child_class: Type, parent_class: Type) int | None#

Calculate the inheritance path length between two classes. Every inheritance level that lies between child_class and parent_class increases the length by one. In case of multiple inheritance, the path length is calculated for each branch and the minimum is returned.

Parameters:
  • child_class – The child class.

  • parent_class – The parent class.

Returns:

The minimum path length between child_class and parent_class or None if no path exists.

krrood.utils.module_and_class_name(t: Type | _SpecialForm) str#
krrood.utils.get_default_value(dataclass_type, field_name)#

Return the default value for a given field in a dataclass.

Parameters:
  • dataclass_type – The dataclass type to get the default value for.

  • field_name – The name of the field to get the default value for.

Returns:

The default value for the field.

krrood.utils.get_default_values_for_dataclass(dataclass_type)#

Return a dict mapping field names to their default values. Only includes fields that actually define a default.

Parameters:

dataclass_type – The dataclass type to get the default values for.

Returns:

A dict mapping field names to their default values.