ranjg.factories package

Module contents

The package of factories.

A factory is constructed with a schema and generate values according to the schema.

Generating values according to the schema can also be accomplished using ranjg.gen, but each time ranjg.gen is run, a Factory is internally generated. Therefore, if you want to generate values repeatedly with the same schema, it is recommended to generate the Factory only once and use its method gen repeatedly.

Examples

The following code is most simple usage.

>>> import ranjg
>>> schema_dict = { 'type': 'string' }
>>> factory = ranjg.Factory(schema_dict)    # -> A factory according to the schema
>>> generated_1 = factory.gen()  # -> A value according to the schema
>>> generated_2 = factory.gen()  # -> A value according to the schema (Almost certainly different than before.)
>>> generated_3 = factory.gen()  # It can be generated as many times as you want.

factory.gen can receive a keyword argument options. See also Options to know about options.

class ranjg.factories.Factory(*args, **kwargs)

Bases: abc.ABC, Generic[_T]

Returns a Factory instance according to the schema.

Parameters
  • schema (dict, optional) – JSON schema object. See also ranjg-JSON-schema.

  • schema_is_validated (bool, optional) – Whether the schema is already validated or not. (In normal usage, this argument is not specified.)

  • context (SchemaContext, optional) – The context of factory construction. (In normal usage, this argument is not specified.)

  • gen_type (str, optional) – If specified, ignore schema.type and create a factory that generates values of the specified type. (In normal usage, this argument is not specified.)

Returns

A factory to generate values according the schema.

Raises

SchemaConflictError – When the schema specified as arguments has conflict. In other words, when no value can satisfy the schema.

abstract gen(*, options: Optional[ranjg.options.Options] = None, context: Optional[ranjg._context.GenerationContext] = None) _T

Generate value according to the schema specified for the factory construction.

Parameters
  • options (Options, optional) – The options for generation.

  • context (GenerationContext, optional) – The context of generation. (In normal usage, this argument is not specified. This argument is for using this function recursively.)

Returns

Generated something.

Raises

GenerateError – If an unforeseen error arises.

gen_as_child(*, options: ranjg.options.Options, parent_context: ranjg._context.GenerationContext, child_key: Union[str, int]) _T

Generate value as another dict or list.

It is wrapper of Factory#gen for ranjg development since it is called in ListFactory#gen etc.. Normally, there is no need for users of ranjg to use this method.

This method is provided for the purpose of not creating bugs in child element generation. As part of this, to prevent omission of argument specification, no default argument is provided.

In order to fulfill the purpose of this method’s existence, this method should be used when creating child elements in ranjg.

Parameters
  • options (Options) – The options for generation. Usually, the options used to generate the parent element is specified as is.

  • parent_context (GenerationContext) – The context of construction. Usually, the context used to generate the parent element is specified as is.

  • child_key (str|int) – The path to the generated element from the parent element. If the parent element is a dict, it is a string; if it is a list, it is an integer.

Returns

Generated something.

property schema_is_validated: bool
validate_schema() None

validate schema

It determines if the schema is free of irregularities.

Raises

InvalidSchemaError – When the schema is invalid