simple_grammar module¶
Most basic grammar implementation.
- class gemseo.core.grammars.simple_grammar.SimpleGrammar(name, names_to_types=None, required_names=None, **kwargs)[source]¶
Bases:
BaseGrammar
A grammar only based on names and types with a dictionary-like interface.
The grammar could be empty, in that case the data validation always pass. If the type bound to a name is
None
then the type of the corresponding data name is always valid.Initialize self. See help(type(self)) for accurate signature.
- Parameters:
name (str) – The name of the grammar.
names_to_types (NamesToTypes | None) – The mapping defining the data names as keys, and data types as values. If
None
, the grammar is empty.required_names (Iterable[str] | None) – The names of the required elements. If
None
, all elements are required.**kwargs (Any) – These arguments are not used.
- Raises:
ValueError – If the name is empty.
- add_namespace(name, namespace)¶
Add a namespace prefix to an existing grammar element.
The updated element name will be
namespace``+:data:`~gemseo.core.namespaces.namespace_separator`+``name
.
- 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. ¶
- 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 ¶
- update(grammar, exclude_names=None)[source]¶
Update the elements from another grammar or elements or names.
When elements are not provided with a
BaseGrammar
, for consistency with__init__
behavior it is assumed that all of them are required.- Parameters:
grammar (BaseGrammar | Iterable[str] | NamesToTypes) – The grammar or names to update from.
exclude_names (Container[str] | None) – The names of the elements that shall not be updated.
- Raises:
TypeError – If
grammar
type is not supported.ValueError – If the elements types are bad.
- Return type:
None
- 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 ¶
- from_namespaced: NamespacesMapping¶
The mapping from element names with namespace prefix to element names without namespace prefix.
- to_namespaced: NamespacesMapping¶
The mapping from element names without namespace prefix to element names with namespace prefix.