pydantic_grammar module¶
A grammar based on a pydantic model.
- class gemseo.core.grammars.pydantic_grammar.PydanticGrammar(name, model=None, **kwargs)[source]¶
Bases:
BaseGrammar
A grammar based on a pydantic model.
The pydantic model passed to the grammar is used to initialize the grammar defaults. Currently, changing the defaults will not update the model.
Initialize self. See help(type(self)) for accurate signature.
- Parameters:
name (str) – The name of the grammar.
model (ModelType | None) – A pydantic model. If
None
, the model will be empty.**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
.
- clear()¶
Empty the grammar.
- Return type:
None
- copy()¶
Create a shallow copy.
- Returns:
The shallow copy.
- Return type:
Self
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- rename_element(current_name, new_name)¶
Rename an element.
- restrict_to(names)¶
Restrict the grammar to the given names.
- to_simple_grammar()[source]¶
Convert the grammar to a
SimpleGrammar
.- Returns:
A
SimpleGrammar
version of the current grammar.- Return type:
Notes
For the elements for which types definitions cannot be expressed as a unique Python type, the type is set to
None
.
- update(grammar, exclude_names=())[source]¶
Update the grammar from another grammar.
- Parameters:
grammar (PydanticGrammar) – 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)¶
Update the grammar from name-value pairs.
The updated elements are required.
- Parameters:
data (Data) – The data from which to get the names and types, typically
{element_name: element_value}
.- Return type:
None
- update_from_names(names)[source]¶
Update the grammar from names.
The updated elements are required and bind the names to Numpy arrays.
- update_from_types(names_to_types, merge=False)[source]¶
Update the grammar from names bound to types.
The updated elements are required. For convenience, when a type is exactly
ndarray
, it is automatically converted toNDArrayPydantic
.
- validate(data, raise_exception=True)¶
Validate data against the grammar.
- Parameters:
data (Data) – 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:
InvalidDataError – If the validation fails and
raise_exception
isTrue
.- Return type:
None
- values() an object providing a view on D's values ¶
- DATA_CONVERTER_CLASS: ClassVar[str] = 'PydanticGrammarDataConverter'¶
The class or the class name of the data converter.
- property data_converter: BaseDataConverter¶
The converter of data values to NumPy arrays and vice versa.
- 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.