gemseo.core.grammars.json_grammar module#
A Grammar based on JSON schema.
- class JSONGrammar(name, file_path='', descriptions=mappingproxy({}), **kwargs)[source]#
Bases:
BaseGrammar
A grammar based on a JSON schema.
For the dictionary-like methods similar to
update
, when a key exists in both grammars, the values can be merged instead of being updated by passingmerge = True
. In that case, the resulting grammar will allow any of the values.Warning
A name can be prefixed by a namespace. Never add a namespace by any other means than the method
add_namespace()
.Notes
Contrary to the standard dictionary, the :meth:
.copy
method creates a deep copy.Initialize self. See help(type(self)) for accurate signature.
- Parameters:
name (str) -- The name of the grammar.
file_path (str | Path) --
The path to a JSON schema file. If empty, do not initialize the grammar from a JSON schema file.
By default it is set to "".
descriptions (Mapping[str, str]) --
The descriptions of the elements read from
file_path
, in the form:{element_name: element_meaning}
. If empty, use the descriptions available in the JSON schema if any.By default it is set to {}.
**kwargs (Any) -- These arguments are not used.
- Raises:
ValueError -- If the name is empty.
- to_json(*args, **kwargs)[source]#
Return the JSON representation of the grammar schema.
- Parameters:
*args (Any) -- The positional arguments passed to
json.dumps()
.**kwargs (Any) -- The keyword arguments passed to
json.dumps()
.
- Returns:
The JSON representation of the schema.
- Return type:
- update_from_file(path, merge=False)[source]#
Update the grammar from a schema file.
- Parameters:
- Raises:
FileNotFoundError -- If the schema file does not exist.
- Return type:
None
- update_from_schema(schema, merge=False)[source]#
Update the grammar from a json schema.
- Parameters:
schema (Schema) -- The schema to update from.
merge (bool) --
Whether to merge or update the grammar.
By default it is set to False.
- Return type:
None
- DATA_CONVERTER_CLASS: ClassVar[str] = 'JSONGrammarDataConverter'#
The class or the class name of the data converter.
- property descriptions: GrammarProperties#
The mapping from the names to the descriptions.
- property schema: Schema#
The dictionary representation of the schema.