simple_grammar module¶
Most basic grammar implementation.
Classes:
|
A grammar based on the names and types of the elements specified by a dictionary. |
- class gemseo.core.grammars.simple_grammar.SimpleGrammar(name, names_to_types=None, required_names=None, **kwargs)[source]¶
Bases:
gemseo.core.grammars.abstract_grammar.AbstractGrammar
A grammar based on the names and types of the elements specified by a dictionary.
- name¶
The name of the grammar.
- Type
str
Initialize self. See help(type(self)) for accurate signature.
- Parameters
name (str) – The grammar name.
names_to_types (Optional[Mapping[str,type]]) –
The mapping defining the data names as keys, and data types as values. If None, the grammar is empty.
By default it is set to None.
required_names (Optional[Mapping[str,bool]]) –
The mapping defining the required data names as keys, bound to whether the data name is required. If None, all data names are required.
By default it is set to None.
**kwargs (Union[str,Path]) – The options of the grammar.
- Return type
None
Methods:
check
(data[, raise_exception])Check the consistency (name and type) of elements with the grammar.
clear
()Clear the grammar.
Return the names of the elements.
get_type_from_python_type
(python_type)Return the grammar type that corresponds to a given Python type.
get_type_of_data_named
(data_name)Return the element type associated to an element name.
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.
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.
Attributes:
The names of the elements.
The types of the elements.
- check(data, raise_exception=True)[source]¶
Check the consistency (name and type) of elements with the grammar.
- Parameters
data (Mapping[str, Any]) – The elements to be checked.
raise_exception (bool) –
Whether to raise an exception when the elements are invalid.
By default it is set to True.
- Raises
TypeError – If a data type in the grammar is not a type.
If the passed data is not a dictionary. * If a name in the passed data is not in the grammar. * If the type of a value in the passed data does not have the specified type in the grammar for the corresponding name.
- Return type
None
- property data_names¶
The names of the elements.
- property data_types¶
The types of the elements.
- 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
- get_type_of_data_named(data_name)[source]¶
Return the element type associated to an element name.
- Parameters
data_name (str) – The name of the element.
- Returns
The type of the element associated to the passed element name.
- Raises
ValueError – If the name does not correspond to an element name.
- Return type
str
- initialize_from_base_dict(typical_data_dict)[source]¶
Initialize the grammar with types and names from typical elements values.
- Parameters
typical_data_dict (Mapping[str, Any]) – Typical elements values indexed by the elements names.
- Return type
None
- initialize_from_data_names(data_names)¶
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
- 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.
- Raises
TypeError – If the passed grammar is not an
AbstractGrammar
.- 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
input_grammar (gemseo.core.grammars.abstract_grammar.AbstractGrammar) – The grammar to take the elements from.
exclude_grammar (gemseo.core.grammars.abstract_grammar.AbstractGrammar) – The grammar containing the elements not to be taken.
- Raises
TypeError – If a passed grammar is not an
AbstractGrammar
.ValueError – If types are inconsistent between both passed grammars.
- 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