gemseo / core / grammars

json_grammar module

A Grammar based on JSON schema.

Classes:

JSONGrammar(name[, schema_file, schema, …])

A grammar subclass that stores the input or output data types and structure a MDODiscipline using a JSON format It is able to check the inputs and outputs against a JSON schema.

class gemseo.core.grammars.json_grammar.JSONGrammar(name, schema_file=None, schema=None, grammar_type=None, descriptions=None)[source]

Bases: gemseo.core.grammars.abstract_grammar.AbstractGrammar

A grammar subclass that stores the input or output data types and structure a MDODiscipline using a JSON format It is able to check the inputs and outputs against a JSON schema.

Constructor.

:param name : grammar name :param schema : a genson Schema to initialize self :param schema_file : the json schema file

Attributes:

OBJECT_FIELD

PROPERTIES_FIELD

REQUIRED_FIELD

TYPES_MAP

TYPE_FIELD

Methods:

cast_array_to_list(data_dict)

Casts the numpy arrays to lists for dictionary values.

clear()

Clears the data to produce an empty grammar.

get_data_names()

Returns the list of data names.

init_from_schema_file(schema_path[, …])

Set the grammar from a file.

initialize_from_base_dict(typical_data_dict)

Initialize a json grammar with types and names from a typical data entry.

initialize_from_data_names(data_names[, …])

Initializes a JSONGrammar from a list of data.

is_all_data_names_existing(data_names)

Checks if data_names are present in grammar.

is_data_name_existing(data_name)

Checks if data_name is present in grammar.

load_data(data[, raise_exception])

Loads the data dictionary in the grammar and checks it against json schema.

remove_item(item_name)

Removes an item from the grammar.

restrict_to(data_names)

Restrict the grammar to given names.

set_item_value(item_name, item_value)

Sets the value of an item.

to_simple_grammar()

Creates a SimpleGrammar from self, preserving the features if possible Ignores the features of JSONGrammar that are not supported by SimpleGrammar.

update_from(input_grammar)

Adds properties coming from another grammar.

update_from_if_not_in(input_grammar, …)

Adds objects coming from input_grammar if they are not in exclude_grammar.

write_schema([path])

Write the schema to a file.

OBJECT_FIELD = 'object'
PROPERTIES_FIELD = 'properties'
REQUIRED_FIELD = 'required'
TYPES_MAP = {'array': <class 'numpy.ndarray'>, 'boolean': <class 'bool'>, 'float': <class 'float'>, 'integer': <class 'int'>, 'number': <class 'numbers.Number'>, 'string': <class 'str'>}
TYPE_FIELD = 'type'
classmethod cast_array_to_list(data_dict)[source]

Casts the numpy arrays to lists for dictionary values.

Parameters

data_dict – the data dictionary

Returns

The dict with casted arrays

clear()[source]

Clears the data to produce an empty grammar.

get_data_names()[source]

Returns the list of data names.

Returns

the data names, as a dict keys set

init_from_schema_file(schema_path, descriptions=None)[source]

Set the grammar from a file.

Parameters

schema_path – path to the schema file.

initialize_from_base_dict(typical_data_dict, description_dict=None)[source]

Initialize a json grammar with types and names from a typical data entry. The keys of the typical_data_dict will be the names of the data in the grammar. The types of the values of the typical_data_dict will be converted to JSON Schema types and define the properties of the JSON Schema.

Parameters
  • typical_data_dict – a data dictionary with keys as data names and values as a typical value for this data

  • description_dict – dictionary of descriptions, {name:meaning} structure

initialize_from_data_names(data_names, descriptions=None)[source]

Initializes a JSONGrammar from a list of data. All data of the grammar will be set as arrays.

Parameters

data_names – a data names list

is_all_data_names_existing(data_names)[source]

Checks if data_names are present in grammar.

Parameters

data_names – the data names list

Returns

True if all data are in grammar

is_data_name_existing(data_name)[source]

Checks if data_name is present in grammar.

Parameters

data_name – the data name

Returns

True if data is in grammar

load_data(data, raise_exception=True)[source]

Loads the data dictionary in the grammar and checks it against json schema.

Parameters
  • data – the input data

  • raise_exception – if False, no exception is raised when data is invalid (Default value = True)

remove_item(item_name)[source]

Removes an item from the grammar.

Parameters

item_name – the item name to be removed

restrict_to(data_names)[source]

Restrict the grammar to given names.

Parameters

data_names – the names of the data to restrict the grammar to

set_item_value(item_name, item_value)[source]

Sets the value of an item.

Parameters
  • item_name – the item name to be modified

  • item_value – value of the item

to_simple_grammar()[source]

Creates a SimpleGrammar from self, preserving the features if possible Ignores the features of JSONGrammar that are not supported by SimpleGrammar.

Returns

a SimpleGrammar instance

update_from(input_grammar)[source]

Adds properties coming from another grammar.

Parameters

input_grammar – the grammar to take inputs from

update_from_if_not_in(input_grammar, exclude_grammar)[source]

Adds objects coming from input_grammar if they are not in exclude_grammar.

Parameters
  • input_grammar – the grammar to take inputs from

  • exclude_grammar – exclusion grammar

write_schema(path=None)[source]

Write the schema to a file.

Parameters

path – Write to this path, if None then write to a file named after the grammar and with .json extension.