Skip to content

Component factory

component_factory

Classes

ComponentFactory

Bases: BaseFactory

BaseComponent factory to create a component from a name or a class.

Attributes
SEP class-attribute instance-attribute
SEP = '_'

The character used to separate the BaseComponent name from the loadcase.

Methods:
create
create(base_class_name: str, **options: Any) -> Any

Return an instance of a class.

Parameters:

  • base_class_name (str) –

    The name of the family class of component to create.

  • **options (Any, default: {} ) –

    The arguments to be passed to the class constructor.

Returns:

  • Any –

    The instance of the class.

Raises:

  • TypeError –

    If the class cannot be instantiated.

Source code in src\vimseo\core\components\component_factory.py
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
def create(
    self,
    base_class_name: str,
    **options: Any,
) -> Any:
    """Return an instance of a class.

    Args:
        base_class_name: The name of the family class of component to create.
        **options: The arguments to be passed to the class constructor.

    Returns:
        The instance of the class.

    Raises:
        TypeError: If the class cannot be instantiated.
    """

    if base_class_name is None:
        return None

    if "load_case" not in options:
        class_name = base_class_name
    else:
        class_name = base_class_name + self.SEP + options["load_case"].name

    try:
        klass = self.get_class(class_name)
        return klass(**options)
    except TypeError:
        LOGGER.exception(
            "Failed to create class %s with arguments %s",
            base_class_name,
            options,
        )