Exemplo n.º 1
0
    def encode_data(cls, obj, typedef):
        r"""Encode an object's data.

        Args:
            obj (object): Object to encode.
            typedef (dict): Type definition that should be used to encode the
                object.

        Returns:
            string: Encoded object.

        """
        args = ArgsMetaschemaProperty.instance2args(obj)
        kwargs = KwargsMetaschemaProperty.instance2kwargs(obj)
        typedef_args = None
        typedef_kwargs = None
        if isinstance(typedef, dict):
            if 'args' in typedef:
                typedef_args = {'items': typedef['args']}
            if 'kwargs' in typedef:
                typedef_kwargs = {'properties': typedef['kwargs']}
        out = [
            JSONArrayMetaschemaType.encode_data(args, typedef_args),
            JSONObjectMetaschemaType.encode_data(kwargs, typedef_kwargs)
        ]
        return out
Exemplo n.º 2
0
    def validate(cls, obj, raise_errors=False):
        r"""Validate an object to check if it could be of this type.

        Args:
            obj (object): Object to validate.
            raise_errors (bool, optional): If True, errors will be raised when
                the object fails to be validated. Defaults to False.

        Returns:
            bool: True if the object could be of this type, False otherwise.

        """
        # Base not called because every python object should pass validation
        # against the object class
        try:
            ArgsMetaschemaProperty.instance2args(obj)
            return True
        except MetaschemaTypeError:
            if raise_errors:
                raise ValueError("Class dosn't have an input_args attribute.")
            return False
Exemplo n.º 3
0
    def encode_data(cls, obj, typedef):
        r"""Encode an object's data.

        Args:
            obj (object): Object to encode.
            typedef (dict): Type definition that should be used to encode the
                object.

        Returns:
            string: Encoded object.

        """
        args = ArgsMetaschemaProperty.instance2args(obj)
        if isinstance(typedef, dict) and ('args' in typedef):
            typedef_args = {'properties': typedef['args']}
        else:
            typedef_args = None
        return JSONObjectMetaschemaType.encode_data(args, typedef_args)