base_grammar module¶
Base class for validating data structures.
- class gemseo.core.grammars.base_grammar.BaseGrammar(name)[source]
Bases:
Mapping
An abstract base class for grammars with a dictionary-like interface.
A grammar considers a certain type of data defined by mandatory and optional names bound to types. A name-type pair is referred to as a grammar element. A grammar can validate a data from these elements.
Initialize self. See help(type(self)) for accurate signature.
- Parameters:
name (str) – The name of the grammar.
- Raises:
ValueError – If the name is empty.
- add_namespace(name, namespace)[source]
Add a namespace prefix to an existing grammar element.
The updated element name will be
namespace``+:data:`~gemseo.core.namespaces.namespace_separator`+``name
.
- clear()[source]
Empty the grammar.
- Return type:
None
- copy()
Create a shallow copy.
- Returns:
The shallow copy.
- Return type:
Self
- abstract is_array(name, numeric_only=False)[source]
Check if an element is an array.
- Parameters:
- 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:
- rename_element(current_name, new_name)[source]
Rename an element.
- restrict_to(names)[source]
Restrict the grammar to the given names.
- abstract to_simple_grammar()[source]
Convert the grammar to a
SimpleGrammar
.- Returns:
A
SimpleGrammar
version of the current grammar.- Return type:
- abstract 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_data(data)[source]
Update the grammar from name-value pairs.
The updated elements are required.
- abstract update_from_names(names)[source]
Update the grammar from names.
The updated elements are required and bind the names to Numpy arrays.
- abstract update_from_types(names_to_types, merge=False)[source]
Update the grammar from names bound to types.
The updated elements are required.
- validate(data, raise_exception=True)[source]
Validate data against the grammar.
- Parameters:
- Raises:
InvalidDataError – If the validation fails and
raise_exception
isTrue
.- Return type:
None
- property defaults: Defaults
The mapping from the names to the default values, if any.
- from_namespaced: MutableMapping[str, str | List[str]]
The mapping from element names with namespace prefix to element names without namespace prefix.
- name: str
The name of the grammar.
- to_namespaced: MutableMapping[str, str | List[str]]
The mapping from element names without namespace prefix to element names with namespace prefix.