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
.
- abstract convert_to_simple_grammar()[source]¶
Convert the grammar to a
SimpleGrammar
.- Returns:
A
SimpleGrammar
version of the current grammar.- Return type:
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- 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:
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- abstract update(grammar, exclude_names=None)[source]¶
Update the grammar.
- Parameters:
grammar (BaseGrammar | Iterable[str]) – The grammar or names to update from.
exclude_names (Container[str] | None) – The names of the elements that shall not be updated.
- Return type:
None
- abstract validate(data, raise_exception=True)[source]¶
Validate data against the grammar.
- Parameters:
- Raises:
InvalidDataException – If the validation fails and
raise_exception
isTrue
.- Return type:
None
- values() an object providing a view on D's values ¶