gemseo / core / grammars

Show inherited members

simple_grammar module

Most basic grammar implementation.

class gemseo.core.grammars.simple_grammar.SimpleGrammar(name, names_to_types=None, required_names=None, **kwargs)[source]

Bases: BaseGrammar

A grammar only based on names and types with a dictionary-like interface.

The grammar could be empty, in that case the data validation always pass. If the type bound to a name is None then the type of the corresponding data name is always valid.

Initialize self. See help(type(self)) for accurate signature.

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

  • names_to_types (NamesToTypes | None) – The mapping defining the data names as keys, and data types as values. If None, the grammar is empty.

  • required_names (Iterable[str] | None) – The names of the required elements. If None, all the elements are required.

  • **kwargs (Any) – These arguments are not used.

Raises:

ValueError – If the name is empty.

is_array(name, numeric_only=False)[source]

Check if an element is an array.

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

  • numeric_only (bool) –

    Whether to check if the array elements are numbers.

    By default it is set to False.

Returns:

Whether the element is an array. If check_items_number is set to True, then return whether the element is an array and its items are numbers.

Raises:

KeyError – If the element is not in the grammar.

Return type:

bool

to_simple_grammar()[source]

Convert the grammar to a SimpleGrammar.

Returns:

A SimpleGrammar version of the current grammar.

Return type:

SimpleGrammar

update(grammar, exclude_names=())[source]

Update the grammar from another grammar.

Parameters:
  • grammar (BaseGrammar) – The grammar to update from.

  • exclude_names (Iterable[str]) –

    The names of the elements that shall not be updated.

    By default it is set to ().

Return type:

None

update_from_names(names)[source]

Update the grammar from names.

The updated elements are required and bind the names to Numpy arrays.

Parameters:

names (Iterable[str]) – The names to update from.

Return type:

None

update_from_types(names_to_types, merge=False)[source]

Update the grammar from names bound to types.

The updated elements are required.

Parameters:
  • names_to_types (Mapping[str, type | None]) – The mapping defining the data names as keys, and data types as values.

  • merge (bool) –

    Whether to merge or update the grammar.

    By default it is set to False.

Raises:

ValueError – When merge is True, since it is not yet supported for SimpleGrammar.

Return type:

None

Note

For consistency with __init__ behavior, it is assumed that all of them are required.

from_namespaced: NamespacesMapping

The mapping from element names with namespace prefix to element names without namespace prefix.

name: str

The name of the grammar.

property required_names: set[str]

The names of the required elements.

to_namespaced: NamespacesMapping

The mapping from element names without namespace prefix to element names with namespace prefix.