gemseo / core

Hide inherited members

base_factory module

Factory base class.

class gemseo.core.base_factory.BaseFactory[source]

Bases: Generic[T]

A base class for factory of objects.

This factory can create objects from a base class or any of its subclasses that can be imported from the given module sources. The base class and the module sources shall be defined as class attributes of the factory class, for instance:

class AFactory(BaseFactory):
    _CLASS = ABaseClass
    _MODULE_NAMES = (
        "first.module.fully.qualified.name",
        "second.module.fully.qualified.name",
    )

There are 3 sources of modules that can be searched:

  • fully qualified module names (such as gemseo.problems, …),

  • the environment variable “GEMSEO_PATH” may contain the list of directories,

  • GEMSEO plugins, i.e. packages which have declared a setuptools entry point.

A setuptools entry point is declared in a plugin pyproject.toml file, with a section:

[options.entry_points]
gemseo_plugins =
    a-name = plugin_package_name

Above a-name is not used and can be any name, but we advise to use the plugin name.

The plugin entry point searched by the factory could be changed with Factory.PLUGIN_ENTRY_POINT.

If a class, despite being a subclass of the base class, or even the base class itself, does not belong to the modules sources then it is not taken into account by the factory.

The created objects are cached: more calls to the constructor with the same call signature will return the object in cache instead of instantiating a new one.

Return type:

Any

create(class_name, *args, **kwargs)[source]

Return an instance of a class.

Parameters:
  • class_name (str) – The name of the class.

  • **args (Any) – The positional arguments to be passed to the class constructor.

  • **kwargs (Any) – The keyword arguments to be passed to the class constructor.

Returns:

The instance of the class.

Raises:

TypeError – If the class cannot be instantiated.

Return type:

T

get_class(name)[source]

Return a class from its name.

Parameters:

name (str) – The name of the class.

Returns:

The class.

Raises:

ImportError – If the class is not available.

Return type:

type[T]

get_default_option_values(name)[source]

Return the constructor kwargs default values of a class.

Parameters:

name (str) – The name of the class.

Returns:

The mapping from the argument names to their default values.

Return type:

dict[str, str | int | float | bool]

get_default_sub_option_values(name, **options)[source]

Return the default values of the sub options of a class.

Parameters:
  • name (str) – The name of the class.

  • **options (str) – The options to be passed to the class required to deduce the sub options.

Returns:

The JSON grammar.

Return type:

JSONGrammar

get_library_name(name)[source]

Return the name of the library related to the name of a class.

Parameters:

name (str) – The name of the class.

Returns:

The name of the library.

Return type:

str

get_options_doc(name)[source]

Return the constructor documentation of a class.

Parameters:

name (str) – The name of the class.

Returns:

The mapping from the argument names to their documentation.

Return type:

dict[str, str]

get_options_grammar(name, write_schema=False, schema_path='')[source]

Return the options JSON grammar for a class.

Attempt to generate a JSONGrammar from the arguments of the __init__ method of the class.

Parameters:
  • name (str) – The name of the class.

  • write_schema (bool) –

    If True, write the JSON schema to a file.

    By default it is set to False.

  • schema_path (Path | str) –

    The path to the JSON schema file. If None, the file is saved in the current directory in a file named after the name of the class.

    By default it is set to “”.

Returns:

The JSON grammar.

Return type:

JSONGrammar

get_sub_options_grammar(name, **options)[source]

Return the JSONGrammar of the sub options of a class.

Parameters:
  • name (str) – The name of the class.

  • **options (str) – The options to be passed to the class required to deduce the sub options.

Returns:

The JSON grammar.

Return type:

JSONGrammar

is_available(name)[source]

Return whether a class can be instantiated.

Parameters:

name (str) – The name of the class.

Returns:

Whether the class can be instantiated.

Return type:

bool

update()[source]

Search for the classes that can be instantiated.

The search is done in the following order:
  1. The fully qualified module names

  2. The plugin packages

  3. The packages from the environment variables

Return type:

None

PLUGIN_ENTRY_POINT: ClassVar[str] = 'gemseo_plugins'

The name of the setuptools entry point for declaring plugins.

property class_names: list[str]

The sorted names of the available classes.

failed_imports: dict[str, str]

The class names bound to the import errors.

Examples using BaseFactory

Pareto front on Binh and Korn problem

Pareto front on Binh and Korn problem