gemseo / core / grammars

Show inherited members

json_grammar module

A Grammar based on JSON schema.

class gemseo.core.grammars.json_grammar.JSONGrammar(name, file_path='', descriptions=None, **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 passing merge = True. In that case, the resulting grammar will allow any of the values.

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 None, do not initialize the grammar from a JSON schema file.

    By default it is set to “”.

  • descriptions (Mapping[str, str] | None) – The descriptions of the elements read from file_path, in the form: {element_name: element_meaning}. If None, use the descriptions available in the JSON schema if any.

  • **kwargs (Any) – These arguments are not used.

Raises:

ValueError – If the name is empty.

is_array(name, numeric_only=False)[source]

Check if an element is an array.

Parameters:
  • name (str) – The name of the element.

  • numeric_only (bool) –

    Whether to check if the array elements are numbers.

    By default it is set to False.

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:

bool

set_descriptions(descriptions)[source]

Set the properties descriptions.

Parameters:

descriptions (Mapping[str, str]) – The descriptions, mapping properties names to the description.

Return type:

None

to_file(path='')[source]

Write the grammar ,schema to a json file.

Parameters:

path (Path | str) –

The file path. If empty, write to a file named after the grammar and with .json extension.

By default it is set to “”.

Return type:

None

to_json(*args, **kwargs)[source]

Return the JSON representation of the grammar schema.

Parameters:
Returns:

The JSON representation of the schema.

Return type:

str

to_simple_grammar()[source]

Convert the grammar to a SimpleGrammar.

Returns:

A SimpleGrammar version of the current grammar.

Return type:

SimpleGrammar

update(grammar, exclude_names=(), merge=False)[source]

Update the elements from another grammar or names or a schema.

Parameters:
  • grammar (JSONGrammar) – 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 ().

  • merge (bool) –

    By default it is set to False.

Raises:

TypeError – If the grammar is not a JSONGrammar.

Return type:

None

update_from_data(data, merge=False)[source]

Update the grammar from name-value pairs.

The updated elements are required.

Parameters:
  • data (Mapping[str, Any]) – The data from which to get the names and types, typically {element_name: element_value}.

  • merge (bool) –

    Whether to merge or update the grammar.

    By default it is set to False.

Return type:

None

Notes

The types of the values of the data will be converted to JSON Schema types and define the elements of the JSON Schema.

update_from_file(path, merge=False)[source]

Update the grammar from a schema file.

Parameters:
  • path (str | Path) – The path to the schema file.

  • merge (bool) –

    Whether to merge or update the grammar.

    By default it is set to False.

Raises:

FileNotFoundError – If the schema file does not exist.

Return type:

None

update_from_names(names, merge=False)[source]

Update the grammar from names.

The updated elements are required and bind the names to Numpy arrays.

Parameters:
  • names (Iterable[str]) – The names to update from.

  • merge (bool) –

    Whether to merge or update the grammar.

    By default it is set to False.

Return type:

None

update_from_schema(schema, merge=False)[source]

Update the grammar from a json schema.

Parameters:
Return type:

None

update_from_types(names_to_types, merge=False)[source]

Update the grammar from names bound to types.

The updated elements are required.

Parameters:
  • names_to_types (Mapping[str, type]) – The mapping defining the data names as keys, and data types as values.

  • merge (bool) –

    Whether to merge or update the grammar.

    By default it is set to False.

Return type:

None

from_namespaced: NamespacesMapping

The mapping from element names with namespace prefix to element names without namespace prefix.

name: str

The name of the grammar.

property required_names: set[str]

The names of the required elements.

property schema: Mapping[str, str | float | bool | Sequence[str | float | bool] | List[str | float | bool | Sequence[str | float | bool]] | DictSchemaType]

The dictionary representation of the schema.

to_namespaced: NamespacesMapping

The mapping from element names without namespace prefix to element names with namespace prefix.

Examples using JSONGrammar

Merge or update a JSONGrammar

Merge or update a JSONGrammar