Beispiel #1
0
    def serialize(self,
                  encoding: str,
                  *,
                  include: Optional[Set[str]] = None,
                  exclude: Optional[Set[str]] = None,
                  skip_defaults: bool = False) -> Union[bytes, str]:
        """Generates a serialized representation of the model

        Parameters
        ----------
        encoding : str
            The serialization type, available types are: {'json', 'json-ext', 'msgpack-ext'}
        include : Optional[Set[str]], optional
            Fields to be included in the serialization.
        exclude : Optional[Set[str]], optional
            Fields to be excluded in the serialization.
        skip_defaults : bool, optional
            If True, skips fields that have default values provided.

        Returns
        -------
        Union[bytes, str]
            The serialized model.
        """
        data = self.dict(include=include,
                         exclude=exclude,
                         skip_defaults=skip_defaults)

        return serialize(data, encoding=encoding)
Beispiel #2
0
    def dict(self,
             *,
             ser_kwargs: Dict[str, Any] = {},
             **kwargs: Dict[str, Any]) -> Dict[str, Any]:
        """Returns object fields as a dictionary.

        Parameters
        ----------
        ser_kwargs: Optional[Dict[str, Any]]
            Additional keyword arguments to pass to serialize.
        **kwargs: Optional[Dict[str, Any]]
            Additional keyword arguments, allow which fields to include, exclude, etc.

        Returns
        -------
        Dict[str, Any]
            Fields as a dictionary.
        """
        encoding = kwargs.pop("encoding", None)

        kwargs["exclude"] = (
            kwargs.get("exclude", None) or
            set()) | self.__config__.serialize_default_excludes  # type: ignore
        kwargs.setdefault(
            "exclude_unset",
            self.__config__.serialize_skip_defaults)  # type: ignore
        if self.__config__.force_skip_defaults:  # type: ignore
            kwargs["exclude_unset"] = True

        data = super().dict(**kwargs)

        if encoding is None:
            return data
        elif encoding == "json":
            return json.loads(serialize(data, encoding=encoding, **ser_kwargs))
        elif encoding == "yaml":
            yaml = yaml_import(raise_error=True)
            return yaml.safe_load(
                serialize(data, encoding=encoding, **ser_kwargs))
        else:
            raise KeyError(
                f"Unknown encoding type '{encoding}', valid encoding types: 'json', 'yaml'."
            )
Beispiel #3
0
    def serialize(
        self,
        encoding: str,
        *,
        include: Optional[Set[str]] = None,
        exclude: Optional[Set[str]] = None,
        exclude_unset: Optional[bool] = None,
        exclude_defaults: Optional[bool] = None,
        exclude_none: Optional[bool] = None,
        **kwargs: Optional[Dict[str, Any]],
    ) -> Union[bytes, str]:
        """Generates a serialized representation of the model

        Parameters
        ----------
        encoding : str
            The serialization type, available types are: {'json', 'json-ext', 'msgpack-ext'}
        include : Optional[Set[str]], optional
            Fields to be included in the serialization.
        exclude : Optional[Set[str]], optional
            Fields to be excluded in the serialization.
        exclude_unset : Optional[bool], optional
            If True, skips fields that have default values provided.
        exclude_defaults: Optional[bool], optional
            If True, skips fields that have set or defaulted values equal to the default.
        exclude_none: Optional[bool], optional
            If True, skips fields that have value ``None``.
        **kwargs: Optional[Dict[str, Any]]
            Additional keyword arguments to pass to serialize.

        Returns
        -------
        Union[bytes, str]
            The serialized model.
        """

        fdargs = {}
        if include:
            fdargs["include"] = include
        if exclude:
            fdargs["exclude"] = exclude
        if exclude_unset:
            fdargs["exclude_unset"] = exclude_unset
        if exclude_defaults:
            fdargs["exclude_defaults"] = exclude_defaults
        if exclude_none:
            fdargs["exclude_none"] = exclude_none

        data = self.dict(**fdargs)

        return serialize(data, encoding=encoding, **kwargs)
Beispiel #4
0
    def dict(self, **kwargs) -> Dict[str, Any]:
        encoding = kwargs.pop("encoding", None)

        kwargs["exclude"] = (
            kwargs.get("exclude", None) or set()
        ) | self.__config__.serialize_default_excludes  # type: ignore
        kwargs.setdefault("exclude_unset", self.__config__.serialize_skip_defaults)  # type: ignore
        if self.__config__.force_skip_defaults:  # type: ignore
            kwargs["exclude_unset"] = True

        data = super().dict(**kwargs)

        if encoding is None:
            return data
        elif encoding == "json":
            return json.loads(serialize(data, encoding="json"))
        else:
            raise KeyError(f"Unknown encoding type '{encoding}', valid encoding types: 'json'.")
Beispiel #5
0
    def serialize(
        self,
        encoding: str,
        *,
        include: Optional[Set[str]] = None,
        exclude: Optional[Set[str]] = None,
        exclude_unset: Optional[bool] = None,
    ) -> Union[bytes, str]:
        """Generates a serialized representation of the model

        Parameters
        ----------
        encoding : str
            The serialization type, available types are: {'json', 'json-ext', 'msgpack-ext'}
        include : Optional[Set[str]], optional
            Fields to be included in the serialization.
        exclude : Optional[Set[str]], optional
            Fields to be excluded in the serialization.
        exclude_unset : Optional[bool], optional
            If True, skips fields that have default values provided.

        Returns
        -------
        Union[bytes, str]
            The serialized model.
        """

        kwargs = {}
        if include:
            kwargs["include"] = include
        if exclude:
            kwargs["exclude"] = exclude
        if exclude_unset:
            kwargs["exclude_unset"] = exclude_unset

        data = self.dict(**kwargs)

        return serialize(data, encoding=encoding)
Beispiel #6
0
    def write(self, data):
        if not isinstance(data, (str, bytes)):
            data = serialize(data, self.encoding)

        return super().write(data)
for driver in drivers:
    folder_name = os.path.join(
        temporary_directory,
        f"{model['method'].lower()}_{molecule_name}_{driver}")
    os.makedirs(folder_name, exist_ok=True)

    input_model = {
        "molecule": qcng.get_molecule(molecule_name),
        "keywords": keywords,
        "model": model,
        "driver": driver,
    }
    input_model = qcel.models.ResultInput(**input_model)

    with open(os.path.join(folder_name, "input.json"), "w") as handle:
        handle.write(serialize(input_model, 'json'))

    # Save generated infiles
    prog = qcng.get_program(program)
    inputs = prog.build_input(input_model, config)
    with open(os.path.join(folder_name, "infiles.msgpack"), "wb") as handle:
        handle.write(serialize(inputs["infiles"], 'msgpack-ext'))

    # Save outfiles
    ret = prog.execute(inputs)
    with open(os.path.join(folder_name, "outfiles.msgpack"), "wb") as handle:
        handle.write(serialize(ret[1]["outfiles"], 'msgpack-ext'))

    # Save result
    result = prog.parse_output(ret[1]["outfiles"], input_model)
    with open(os.path.join(folder_name, "output.json"), "w") as handle: