object_parser package

Submodules

object_parser.errors module

exception object_parser.errors.BuildError[source]

Bases: SpecError

exception object_parser.errors.BuildErrors[source]

Bases: SpecError

class object_parser.errors.ErrorMessages[source]

Bases: object

A static class with can be subclassed

static invalid_key_format(cls, key: str)[source]
static missing_mandatory_key(cls, key: str) str[source]
static no_type_annotations(cls) str[source]
static unexpected_key(cls, key)[source]
exception object_parser.errors.SpecError[source]

Bases: Exception

exception object_parser.errors.SpecErrors[source]

Bases: SpecError

object_parser.errors.to_string(errors: BuildErrors) str[source]

object_parser.factory module

class object_parser.factory.Factory(cls: type, parent_keys: ~typing.List[str] = None, *, errors=<class 'mash.object_parser.errors.ErrorMessages'>)[source]

Bases: ABC

An interface for instantiating objects from json-like data.

build(data={})[source]

Initialize self.cls with fields from data. All child objects are recursively instantiated as well, based on type annotations.

See object_parser_example.py for a larger usecase example.

Raises

  • BuildError (for a single field)

  • BuildErrors (for multiple invalid fields).

User-defineable methods

Process values - cls.parse_value() can be used to pre-process input values before instantiating objects - cls.__post_init__() can be used to check an object after initialization

Process keys - cls.parse_key() can be used to pre-process input keys. - cls.verify_key_format() defaults to verify_key_format - cls._key_synonyms: dict can be used to define alternative keys

abstract build_instance(data) object[source]

Build an instance of self.cls, after parsing input data but before finializing.

property error_prefix: str
property parent_path: str
parse_value(value) object[source]

Use the optional method “parse_value” in cls to parse a value.

verify_key_format(key: str)[source]

Verify key format. Either using the optional method “parse_value” in cls, or otherwise using a default verification.

class object_parser.factory.JSONFactory(cls: type, parent_keys: ~typing.List[str] = None, *, errors=<class 'mash.object_parser.errors.ErrorMessages'>)[source]

Bases: Factory

build_enum(value) Enum[source]
build_field(key, data)[source]
build_fields(data={}) dict[source]

Instantiate all fields in cls, based its type annotations and the values in data.

build_from_dict(fields: dict)[source]
build_generic_Dict(fields)[source]
build_instance(data) object[source]

Init either a dataclass, list, Enum or custom class.

build_list(items: list) list[source]
build_object(data) object[source]
object_parser.factory.build(cls: type, json: dict)[source]

Initialize cls with fields from data.

object_parser.oas module

Generate a OAS/Swagger component See: [OAS](https://swagger.io/specification/)

class object_parser.oas.K[source]

Bases: object

OAS keys

components = 'components'
doc = 'description'
props = 'properties'
schemas = 'schemas'
class object_parser.oas.OAS(*args, **kwds)[source]

Bases: dict

A key-value map that represents an OAS. It that can be converted to JSON or YAML and viewed in: https://editor.swagger.io/

add_array(parent_name: str, child_name: str, child)[source]
property components
extend(obj: object)[source]

Generate OAS/Swagger components from a class See: [OAS](https://swagger.io/specification/) E.g.

components:
    schemas:
        User:
            properties:
                id:
                    type: integer
                name:
                    type: string
class object_parser.oas.V[source]

Bases: object

OAS values

static ref(item='')[source]
object_parser.oas.has_known_type(obj)[source]
object_parser.oas.infer_oas_type(obj)[source]
object_parser.oas.oas_component(obj, doc='')[source]
object_parser.oas.oas_ref(item='')[source]
object_parser.oas.path_create(item_type: str, verb='POST')[source]

object_parser.object_parser module

Parse json-like data and init objects such as dataclasses.

# Classes

  • factory.Factory is a generic interface.

  • factory.JSONFactory is a concrete implementation.

  • errors.ErrorMessages exposes a few custom strings.

object_parser.object_parser.find_synonym(cls, key: str)[source]
object_parser.object_parser.parse_field_key(cls, key: str) str[source]
object_parser.object_parser.parse_field_keys(cls, data) dict[source]
object_parser.object_parser.verify_key_format(cls, key: str)[source]

object_parser.object_parser_standards module

class object_parser.object_parser_standards.A(x: int = 0, y: int = 1, z: int = 10)[source]

Bases: object

x: int = 0
y: int = 1
z: int = 10
class object_parser.object_parser_standards.B(left: object_parser.object_parser_standards.A, right: object_parser.object_parser_standards.A, top: object_parser.object_parser_standards.A, bottom: object_parser.object_parser_standards.A)[source]

Bases: object

bottom: A
left: A
right: A
top: A
class object_parser.object_parser_standards.C(a: object_parser.object_parser_standards.A, b: object_parser.object_parser_standards.B, x: int, y: float, z: List[object_parser.object_parser_standards.A])[source]

Bases: object

a: A
b: B
x: int
y: float
z: List[A]
object_parser.object_parser_standards.dictionary_mapping()[source]
object_parser.object_parser_standards.differencing()[source]

Module contents