ranjg package
Subpackages
Module contents
- ranjg.gen(schema: Optional[dict] = None, *, schema_file: Optional[str] = None, output_file: Optional[str] = None, output_file_list: Optional[Iterable[str]] = None, output_fp: Optional[TextIO] = None, output_fp_list: Optional[Iterable[TextIO]] = None, options: Optional[ranjg.options.Options] = None, options_file: Optional[str] = None, multiplicity: Optional[int] = None, schema_is_validated: bool = False, return_none: bool = False, context: Optional[ranjg._context.GenerationContext] = None)
Generate something randomly according to the JSON schema.
This function is not fully compliant with the JSON schema, and unsupported parameters in the schema are ignored. See also ranjg-JSON-schema to explore the supported parameters.
- Examples
The following code is most simple usage.
>>> import ranjg >>> schema_dict = { 'type': 'string' } >>> ranjg.gen(schema_dict) # -> returns some string
By replacing the contents of
schema_dict
, you can change the value to be generated.If you want to specify a schema without a python dict but with JSON file, you can use argument
schema_file
.>>> import ranjg >>> schema_path = './schema_file.json' >>> ranjg.gen(schema_file=schema_path) # -> returns some value
If you want to get the result as JSON file, you can use argument
output_file
oroutput_fp
. (Following two examples will run similarly.>>> import ranjg >>> schema_dict = { 'type': 'string' } >>> ranjg.gen(schema_dict, output_file='./output.json') >>> # -> returns the result value and writes the result to specified file
>>> import ranjg >>> schema_dict = { 'type': 'string' } >>> with open('./output.json', 'w+') as out_fp: >>> ranjg.gen(schema_dict, output_fp=out_fp) >>> # -> returns the result value and writes the result to specified file
- Parameters
schema (dict, optional) – JSON schema object. See also ranjg-JSON-schema. Only one of this argument or
schema_file
needs to be specified.schema_file (str, optional) – The path to JSON schema file. This JSON schema is used instead of the argument
schema
.output_file (str, optional) – The path to a file where the result will be output as JSON. If
multiplicity
is specified, a list consisting of the generated values will be output as json.output_file_list (Iterable[str], optional) – The list of paths to a file where the result will be output as JSON. It repeats the generation and outputs each result to each file.
output_fp (TextIO, optional) – The writing object of a file where the result will be output as JSON. If
multiplicity
is specified, a list consisting of the generated values will be output as json.output_fp_list (Iterable[TextIO], optional) – The list of writing objects of files where the result will be output as JSON. It repeats the generation and outputs each result to each file.
options (Options, optional) – The options for generation.
options_file (str, optional) – The path to options file. This is parsed as JSON to an Options instance.
multiplicity (int, optional) – If specified, it repeats the generation for the specified number of times and returns the results as a list.
schema_is_validated (bool, optional) – Whether the schema is already validated or not. (In normal usage, this argument is not specified.)
return_none (bool, default=False) – If it is True, this function returns None. (If it is False, the result contains all outputs. In particular, if it is repeated a lot, such as when a large list is specified in output_file_list, it may overwhelm memory.)
context (GenerationContext) – The context of construction. (In normal usage, this argument is not specified. This argument is for using this function recursively.)
- Returns
Generated something. It is satisfies the JSON schema. However, if
multiplicity
is specified, it returns a list, each element of which satisfies the schema.- Raises
SchemaFileIOError – When loading schema_file is failed
OptionsFileIOError – When loading options_file is failed
InvalidSchemaError – When the schema specified as arguments is invalid.
SchemaConflictError – When the schema specified as arguments has confliction. In other words, when no value can satisfy the schema.
GenerateError – If an unforeseen error arises.