gemseo / third_party / fastjsonschema

draft04 module

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

Bases: gemseo.third_party.fastjsonschema.generator.CodeGenerator

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-5]\\d|Z)?\\Z', 'email': '^[^@]+@[^@]+\\.[^@]+\\Z', 'hostname': '^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]{0,61}[A-Za-z0-9])\\Z', 'ipv4': '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\Z', 'ipv6': '^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[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-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})?::(?:[0-9A-Fa-f]{1,4}:){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}:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}(?:[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}:){,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'}
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_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_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