gemseo /
core /
grammars
base_grammar module
Base class for validating data structures.
-
class gemseo.core.grammars.base_grammar.BaseGrammar(name)[source]
Bases: collections.abc.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.
- Return type
None
-
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.
- Parameters
-
- Return type
None
-
abstract clear()[source]
Empty the grammar.
- Return type
None
-
abstract convert_to_simple_grammar()[source]
Convert the grammar to a SimpleGrammar.
- Returns
A SimpleGrammar version of the current grammar.
- Return type
SimpleGrammar
-
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
-
abstract is_array(name)[source]
Check whether an element type shall be an array.
- Parameters
name (str) – The name of the element.
- Returns
Whether the element type shall be an array.
- Raises
KeyError – If the element is not in the grammar.
- Return type
bool
-
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 rename_element(current_name, new_name)[source]
Rename an element.
- Parameters
-
- Return type
None
-
abstract restrict_to(names)[source]
Restrict the grammar to the given names.
- Parameters
names (Sequence[str]) – The names of the elements to restrict the grammar to.
- Raises
KeyError – If a name is not in the grammar.
- Return type
None
-
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.
By default it is set to None.
- Return type
None
-
abstract update_from_data(data)[source]
Update the grammar from name-value pairs.
- Parameters
data (Mapping[str, Any]) – The data from which to get the names and types,
typically {element_name: element_value}.
- Raises
TypeError – If a value has a bad type.
- Return type
None
-
abstract validate(data, raise_exception=True)[source]
Validate data against the grammar.
- Parameters
data (Mapping[str, Any]) – The data to be checked,
with a dictionary-like format: {element_name: element_value}.
raise_exception (bool) –
Whether to raise an exception when the validation fails.
By default it is set to True.
- Raises
InvalidDataException – If the validation fails and raise_exception is
True.
- Return type
None
-
values() → an object providing a view on D's values
-
from_namespaced: MutableMapping[str, Union[str, List[str]]]
The mapping from element names with namespace prefix to element names
without namespace prefix.
-
name: str
The name of the grammar.
-
property names: KeysView[str]
The names of the elements.
-
abstract property required_names: set[str]
The names of the required elements.
-
to_namespaced: MutableMapping[str, Union[str, List[str]]]
The mapping from element names without namespace prefix to element
names with namespace prefix.