gemseo / core / grammars

abstract_grammar module

Rules and checks for disciplines inputs/outputs validation.

Classes:

AbstractGrammar(name, **kwargs)

The abstraction of a grammar.

class gemseo.core.grammars.abstract_grammar.AbstractGrammar(name, **kwargs)[source]

Bases: object

The abstraction of a grammar.

A grammar is a set of elements characterised by their names and types. One can use it to check if elements values are consistent with it.

It is mainly used to store the names and types of the inputs and outputs of an MDODiscipline.

Grammars supports __contains__ special method, so that one can test if an element is in the grammar with the statement “x” in grammar.

name

The name of the grammar.

Type

str

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

Parameters
  • name (str) – The name to be given to the grammar.

  • **kwargs (Union[str,Path]) – The options of the grammar.

Return type

None

Methods:

clear()

Clear the grammar.

get_data_names()

Return the names of the elements.

get_type_from_python_type(python_type)

Return the grammar type that corresponds to a given Python type.

initialize_from_base_dict(typical_data_dict)

Initialize the grammar with types and names from typical elements values.

initialize_from_data_names(data_names)

Initialize the grammar from the names of the elements and float type.

is_all_data_names_existing(data_names)

Check if the names of the elements are present in the grammar.

is_data_name_existing(data_name)

Check if the name of an element is present in the grammar.

is_required(element_name)

Check if an element is required in the grammar.

is_type_array(data_name)

Check if an element is an array.

load_data(data[, raise_exception])

Load elements values and check their consistency with the grammar.

remove_item(item_name)

Remove an element.

restrict_to(data_names)

Restrict the grammar to the given names.

to_simple_grammar()

Convert to the base SimpleGrammar type.

update_elements([python_typing])

Add or update elements from their names and types.

update_from(input_grammar)

Update the grammar with a second one.

update_from_if_not_in(input_grammar, ...)

Add the elements from a second grammar that are not present in a third one.

update_required_elements(**elements)

Add or update the required elements in the grammar.

clear()[source]

Clear the grammar.

Return type

None

get_data_names()[source]

Return the names of the elements.

Returns

The names of the elements sorted alphabetically.

Return type

List[str]

get_type_from_python_type(python_type)[source]

Return the grammar type that corresponds to a given Python type.

Parameters

python_type (type) – The Python type.

Returns

The equivalent grammar type.

Return type

type

initialize_from_base_dict(typical_data_dict)[source]

Initialize the grammar with types and names from typical elements values.

Parameters

typical_data_dict (Dict[str, numpy.ndarray]) – Typical elements values indexed by the elements names.

Return type

None

initialize_from_data_names(data_names)[source]

Initialize the grammar from the names of the elements and float type.

Parameters

data_names (Iterable[str]) – The names of the elements.

Return type

None

is_all_data_names_existing(data_names)[source]

Check if the names of the elements are present in the grammar.

Parameters

data_names (Iterable[str]) – The names of the elements.

Returns

Whether all the elements names are in the grammar.

Return type

bool

is_data_name_existing(data_name)[source]

Check if the name of an element is present in the grammar.

Parameters

data_name (str) – The name of the element.

Returns

Whether the name of the element is present in the grammar.

Return type

bool

is_required(element_name)[source]

Check if an element is required in the grammar.

Parameters

element_name (str) – The data name to check.

Returns

Whether the element name is required.

Raises

ValueError – If the given element is not in the grammar.

Return type

bool

is_type_array(data_name)[source]

Check if an element is an array.

Parameters

data_name (str) – The name of the element.

Returns

Whether the element is an array.

Raises

ValueError – If the name does not correspond to an element name.

Return type

bool

load_data(data, raise_exception=True)[source]

Load elements values and check their consistency with the grammar.

Parameters
  • data (Mapping[str, Any]) – The elements values to be checked.

  • raise_exception (bool) –

    Whether to raise an exception when the elements values are not consistent with the grammar.

    By default it is set to True.

Returns

The elements values after successful consistency checking.

Return type

Mapping[str, Any]

remove_item(item_name)[source]

Remove an element.

Parameters

item_name (str) – The name of the element to be removed.

Raises

KeyError – When the element is not in the grammar.

Return type

None

restrict_to(data_names)[source]

Restrict the grammar to the given names.

Parameters

data_names (Sequence[str]) – The names of the elements to restrict the grammar to.

Return type

None

to_simple_grammar()[source]

Convert to the base SimpleGrammar type.

Returns

A SimpleGrammar version of the current grammar.

Return type

SimpleGrammar

update_elements(python_typing=False, **elements)[source]

Add or update elements from their names and types.

Parameters
  • python_typing (bool) –

    If True, handle automatically the conversion from Python type to grammar type.

    By default it is set to False.

  • **elements (Mapping[str, type]) – The names to types bindings of the elements to add or update.

Return type

None

Examples

>>> grammar.update_elements(a=str, b=int)
>>> grammar.update_elements(a=str, b=int, python_typing=True)
>>> grammar.update_elements(**names_to_types)
update_from(input_grammar)[source]

Update the grammar with a second one.

Add the new elements and update the existing ones.

Parameters

input_grammar (gemseo.core.grammars.abstract_grammar.AbstractGrammar) – The grammar to take the elements from.

Return type

None

update_from_if_not_in(input_grammar, exclude_grammar)[source]

Add the elements from a second grammar that are not present in a third one.

Parameters
Return type

None

update_required_elements(**elements)[source]

Add or update the required elements in the grammar.

Parameters

**elements (Mapping[str, bool]) – The names of the elements bound to whether they shall be required.

Raises
  • KeyError – If a given element name is not in the grammar.

  • TypeError – If a given element name is not associated to a boolean value.

Return type

None