Ejemplo n.º 1
0
        def from_msgpack(
            cls,
            msgpack_bytes: bytes = None,
            filename: Union[str, PathLike] = None,
            **kwargs,
        ) -> "Box":
            """
            Transforms msgpack bytes or file into a Box object

            :param msgpack_bytes: string to pass to `msgpack.unpackb`
            :param filename: filename to open and pass to `msgpack.unpack`
            :param kwargs: parameters to pass to `Box()`
            :return: Box object
            """
            box_args = {}
            for arg in kwargs.copy():
                if arg in BOX_PARAMETERS:
                    box_args[arg] = kwargs.pop(arg)

            data = _from_msgpack(msgpack_bytes=msgpack_bytes,
                                 filename=filename,
                                 **kwargs)
            if not isinstance(data, dict):
                raise BoxError(
                    f"msgpack data not returned as a dictionary but rather a {type(data).__name__}"
                )
            return cls(data, **box_args)
Ejemplo n.º 2
0
        def from_msgpack(cls,
                         msgpack_bytes: bytes = None,
                         filename: Union[str, PathLike] = None,
                         **kwargs):
            """
            Transforms a toml string or file into a BoxList object

            :param msgpack_bytes: string to pass to `msgpack.packb`
            :param filename: filename to open and pass to `msgpack.pack`
            :param kwargs: parameters to pass to `Box()`
            :return:
            """
            box_args = {}
            for arg in list(kwargs.keys()):
                if arg in BOX_PARAMETERS:
                    box_args[arg] = kwargs.pop(arg)

            data = _from_msgpack(msgpack_bytes=msgpack_bytes,
                                 filename=filename,
                                 **kwargs)
            if not isinstance(data, list):
                raise BoxError(
                    f"msgpack data not returned as a list but rather a {type(data).__name__}"
                )
            return cls(data, **box_args)