gemseo / core

json_grammar module

A Grammar based on JSON schema

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

Bases: gemseo.core.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 :param grammar_type : the type of grammar : input or output

DEFAULTS_FIELD = 'defaults'
OBJECT_FIELD = 'object'
PROPERTIES_FIELD = 'properties'
REQUIRED_FIELD = 'required'
TYPE_FIELD = 'type'
add_description(description_dict)[source]

Add a description to the properties

Parameters

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

static cast_array_to_list(data_dict)[source]

Casts the numpy arrays in data_dict to lists

:param 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_file='input.json')[source]

Initializes grammar from

Parameters

schema_file – path to the schema input file (Default value = “input.json”)

initialize_from_base_dict(typical_data_dict, schema_file=None, write_schema=False, 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

  • schema_file – the output json file path. If None : input.json or output.json depending on grammar type. (Default value = None)

  • write_schema – if True, writes the schema files (Default value = False)

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

initialize_from_data_names(data_names, schema_file=None, write_schema=False)[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

  • schema_file – the output json file path. If None : input.json or output.json depending on grammar type. (Default value = None)

  • write_schema – if True, writes the schema files (Default value = False)

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_dict, raise_exception=True)[source]

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

Parameters
  • data_dict – the input data

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

property properties

Accessor for the properties of the schema

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]

Restricts the grammar to a sublist of data 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

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