Skip to content

base

mlte/qa_category/base.py

The superclass for all model QACategory.

QACategory

The QACategory type represents an abstract model QA category.

Source code in mlte/qa_category/base.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class QACategory(metaclass=abc.ABCMeta):
    """The QACategory type represents an abstract model QA category."""

    @classmethod
    def __subclasshook__(cls, subclass):
        """Define the interface for all concrete quality attribute categories."""
        return meta.has_callables(subclass, "__init__")

    def __init__(
        self,
        instance: QACategory,
        description: str,
        rationale: str,
    ):
        """
        Initialize a QACategory instance.

        :param instance: The derived QACategory we are constructing from.
        :param description: The description of the QACategory
        :param rationale: The rationale for using the QACategory
        """
        self.name: str = instance.__class__.__name__
        """The name of the QACategory."""

        self.description: str = cleantext(description)
        """The description of the QACategory."""

        self.rationale: str = rationale
        """The rationale for using the QACategory."""

        self.module: str = instance.__module__
        """The name of the module the QACategory is defined in."""

    def to_model(self) -> QACategoryModel:
        """
        Return a QACategory as a model.

        :return: The QACategory as its model.
        """
        return QACategoryModel(
            name=self.name,
            description=self.description,
            rationale=self.rationale,
            module=self.module,
        )

    @classmethod
    def from_model(cls, model: QACategoryModel) -> QACategory:
        """
        Load a QACategory instance from a model.

        :param model: The model with the QACategory info.

        :return: The loaded QACategory
        """
        if model.name == "":
            raise RuntimeError(
                "QACategory is malformed, it does not have a valid name."
            )
        classname = model.name

        # Load the class type from the module
        module_path = model.module
        try:
            qa_category_module = importlib.import_module(module_path)
        except Exception:
            raise RuntimeError(f"Module {module_path} not found")
        try:
            class_: Type[QACategory] = getattr(qa_category_module, classname)
        except AttributeError:
            raise RuntimeError(
                f"QACategory {model.name} in module {module_path} not found"
            )

        # Instantiate the QACategory
        return class_(model.rationale)  # type: ignore

description = cleantext(description) instance-attribute

The description of the QACategory.

module = instance.__module__ instance-attribute

The name of the module the QACategory is defined in.

name = instance.__class__.__name__ instance-attribute

The name of the QACategory.

rationale = rationale instance-attribute

The rationale for using the QACategory.

__init__(instance, description, rationale)

Initialize a QACategory instance.

Parameters:

Name Type Description Default
instance QACategory

The derived QACategory we are constructing from.

required
description str

The description of the QACategory

required
rationale str

The rationale for using the QACategory

required
Source code in mlte/qa_category/base.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(
    self,
    instance: QACategory,
    description: str,
    rationale: str,
):
    """
    Initialize a QACategory instance.

    :param instance: The derived QACategory we are constructing from.
    :param description: The description of the QACategory
    :param rationale: The rationale for using the QACategory
    """
    self.name: str = instance.__class__.__name__
    """The name of the QACategory."""

    self.description: str = cleantext(description)
    """The description of the QACategory."""

    self.rationale: str = rationale
    """The rationale for using the QACategory."""

    self.module: str = instance.__module__
    """The name of the module the QACategory is defined in."""

__subclasshook__(subclass) classmethod

Define the interface for all concrete quality attribute categories.

Source code in mlte/qa_category/base.py
21
22
23
24
@classmethod
def __subclasshook__(cls, subclass):
    """Define the interface for all concrete quality attribute categories."""
    return meta.has_callables(subclass, "__init__")

from_model(model) classmethod

Load a QACategory instance from a model.

Parameters:

Name Type Description Default
model QACategoryModel

The model with the QACategory info.

required

Returns:

Type Description
QACategory

The loaded QACategory

Source code in mlte/qa_category/base.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
@classmethod
def from_model(cls, model: QACategoryModel) -> QACategory:
    """
    Load a QACategory instance from a model.

    :param model: The model with the QACategory info.

    :return: The loaded QACategory
    """
    if model.name == "":
        raise RuntimeError(
            "QACategory is malformed, it does not have a valid name."
        )
    classname = model.name

    # Load the class type from the module
    module_path = model.module
    try:
        qa_category_module = importlib.import_module(module_path)
    except Exception:
        raise RuntimeError(f"Module {module_path} not found")
    try:
        class_: Type[QACategory] = getattr(qa_category_module, classname)
    except AttributeError:
        raise RuntimeError(
            f"QACategory {model.name} in module {module_path} not found"
        )

    # Instantiate the QACategory
    return class_(model.rationale)  # type: ignore

to_model()

Return a QACategory as a model.

Returns:

Type Description
QACategoryModel

The QACategory as its model.

Source code in mlte/qa_category/base.py
51
52
53
54
55
56
57
58
59
60
61
62
def to_model(self) -> QACategoryModel:
    """
    Return a QACategory as a model.

    :return: The QACategory as its model.
    """
    return QACategoryModel(
        name=self.name,
        description=self.description,
        rationale=self.rationale,
        module=self.module,
    )