gemseo / third_party / fastjsonschema

draft04 module

Classes:

CodeGeneratorDraft04(definition[, resolver])

class gemseo.third_party.fastjsonschema.draft04.CodeGeneratorDraft04(definition, resolver=None)[source]

Bases: gemseo.third_party.fastjsonschema.generator.CodeGenerator

Attributes:

FORMAT_REGEXS

INDENT

func_code

Returns generated code of whole validation function as string.

global_state

Returns global variables for generating function from func_code.

global_state_code

Returns global variables for generating function from func_code as code.

Methods:

create_variable_is_dict()

Append code for creating variable with bool if it’s instance of list with a name {variable}_is_dict.

create_variable_is_list()

Append code for creating variable with bool if it’s instance of list with a name {variable}_is_list.

create_variable_keys()

Append code for creating variable with keys of that variable (dictionary) with a name {variable}_keys.

create_variable_with_length()

Append code for creating variable with length of that variable (for example length of list or dictionary) with name {variable}_len.

generate_additional_properties()

Means object with keys with values defined by definition.

generate_all_of()

Means that value have to be valid by all of those definitions.

generate_any_of()

Means that value have to be valid by any of those definitions.

generate_dependencies()

Means when object has property, it needs to have also other property.

generate_enum()

Means that only value specified in the enum is valid.

generate_format()

Means that value have to be in specified format.

generate_func_code()

Creates base code of validation function and calls helper for creating code by definition.

generate_func_code_block(definition, …[, …])

Creates validation rules for current definition.

generate_items()

Means array is valid only when all items are valid by this definition.

generate_max_items()

generate_max_length()

generate_max_properties()

generate_maximum()

generate_min_items()

generate_min_length()

generate_min_properties()

generate_minimum()

generate_multiple_of()

generate_not()

Means that value have not to be valid by this definition.

generate_one_of()

Means that value have to be valid by only one of those definitions.

generate_pattern()

generate_pattern_properties()

Means object with defined keys as patterns.

generate_properties()

Means object with defined keys.

generate_ref()

Ref can be link to remote or local definition.

generate_required()

generate_type()

Validation of type.

generate_unique_items()

With Python 3.4 module timeit recommended this solutions:

generate_validation_function(uri, name)

Generate validation function for given uri with given name

l(*args, **kwds)

run_generate_functions(definition)

FORMAT_REGEXS = {'date-time': '^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:[+-][0-2]\\d:[0-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]{1,4}:){,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)\\Z', 'uri': '^\\w+:(\\/?\\/?)[^\\s]+\\Z'}
INDENT = 4
create_variable_is_dict()

Append code for creating variable with bool if it’s instance of list with a name {variable}_is_dict. Similar to create_variable_with_length.

create_variable_is_list()

Append code for creating variable with bool if it’s instance of list with a name {variable}_is_list. Similar to create_variable_with_length.

create_variable_keys()

Append code for creating variable with keys of that variable (dictionary) with a name {variable}_keys. Similar to create_variable_with_length.

create_variable_with_length()

Append code for creating variable with length of that variable (for example length of list or dictionary) with name {variable}_len. It can be called several times and always it’s done only when that variable still does not exists.

property func_code

Returns generated code of whole validation function as string.

generate_additional_properties()[source]

Means object with keys with values defined by definition.

{
    'properties': {
        'key': {'type': 'number'},
    }
    'additionalProperties': {'type': 'string'},
}

Valid object is containing key called ‘key’ and it’s value any number and any other key with any string.

generate_all_of()[source]

Means that value have to be valid by all of those definitions. It’s like put it in one big definition.

{
    'allOf': [
        {'type': 'number'},
        {'minimum': 5},
    ],
}

Valid values for this definition are 5, 6, 7, … but not 4 or ‘abc’ for example.

generate_any_of()[source]

Means that value have to be valid by any of those definitions. It can also be valid by all of them.

{
    'anyOf': [
        {'type': 'number', 'minimum': 10},
        {'type': 'number', 'maximum': 5},
    ],
}

Valid values for this definition are 3, 4, 5, 10, 11, … but not 8 for example.

generate_dependencies()[source]

Means when object has property, it needs to have also other property.

{
    'dependencies': {
        'bar': ['foo'],
    },
}

Valid object is containing only foo, both bar and foo or none of them, but not object with only bar.

Since draft 06 definition can be boolean or empty array. True and empty array means nothing, False means that key cannot be there at all.

generate_enum()[source]

Means that only value specified in the enum is valid.

{
    'enum': ['a', 'b'],
}
generate_format()[source]

Means that value have to be in specified format. For example date, email or other.

{'format': 'email'}

Valid value for this definition is user@example.com but not @username

generate_func_code()

Creates base code of validation function and calls helper for creating code by definition.

generate_func_code_block(definition, variable, variable_name, clear_variables=False)

Creates validation rules for current definition.

generate_items()[source]

Means array is valid only when all items are valid by this definition.

{
    'items': [
        {'type': 'integer'},
        {'type': 'string'},
    ],
}

Valid arrays are those with integers or strings, nothing else.

Since draft 06 definition can be also boolean. True means nothing, False means everything is invalid.

generate_max_items()[source]
generate_max_length()[source]
generate_max_properties()[source]
generate_maximum()[source]
generate_min_items()[source]
generate_min_length()[source]
generate_min_properties()[source]
generate_minimum()[source]
generate_multiple_of()[source]
generate_not()[source]

Means that value have not to be valid by this definition.

{'not': {'type': 'null'}}

Valid values for this definition are ‘hello’, 42, {} … but not None.

Since draft 06 definition can be boolean. False means nothing, True means everything is invalid.

generate_one_of()[source]

Means that value have to be valid by only one of those definitions. It can’t be valid by two or more of them.

{
    'oneOf': [
        {'type': 'number', 'multipleOf': 3},
        {'type': 'number', 'multipleOf': 5},
    ],
}

Valid values for this definition are 3, 5, 6, … but not 15 for example.

generate_pattern()[source]
generate_pattern_properties()[source]

Means object with defined keys as patterns.

{
    'patternProperties': {
        '^x': {'type': 'number'},
    },
}

Valid object is containing key starting with a ‘x’ and value any number.

generate_properties()[source]

Means object with defined keys.

{
    'properties': {
        'key': {'type': 'number'},
    },
}

Valid object is containing key called ‘key’ and value any number.

generate_ref()

Ref can be link to remote or local definition.

{'$ref': 'http://json-schema.org/draft-04/schema#'}
{
    'properties': {
        'foo': {'type': 'integer'},
        'bar': {'$ref': '#/properties/foo'}
    }
}
generate_required()[source]
generate_type()[source]

Validation of type. Can be one type or list of types.

{'type': 'string'}
{'type': ['string', 'number']}
generate_unique_items()[source]

With Python 3.4 module timeit recommended this solutions:

>>> timeit.timeit("len(x) > len(set(x))", "x=range(100)+range(100)", number=100000)
0.5839540958404541
>>> timeit.timeit("len({}.fromkeys(x)) == len(x)", "x=range(100)+range(100)", number=100000)
0.7094449996948242
>>> timeit.timeit("seen = set(); any(i in seen or seen.add(i) for i in x)", "x=range(100)+range(100)", number=100000)
2.0819358825683594
>>> timeit.timeit("np.unique(x).size == len(x)", "x=range(100)+range(100); import numpy as np", number=100000)
2.1439831256866455
generate_validation_function(uri, name)

Generate validation function for given uri with given name

property global_state

Returns global variables for generating function from func_code. Includes compiled regular expressions and imports, so it does not have to do it every time when validation function is called.

property global_state_code

Returns global variables for generating function from func_code as code. Includes compiled regular expressions and imports.

l(*args, **kwds)
run_generate_functions(definition)